Insert data in collection at Meteor's startup

Leon

I would like to insert data at Meteor's startup. (And after from a JSON file)

At startup, I create a new account and I would like to insert data and link it to this account once this one created.

This is the code that creates the new account at startup:

if(!Meteor.users.findOne({emails: { $elemMatch: { address: "[email protected]"}}})){
    var id = Accounts.createUser({ email: "[email protected]", password: "1234", profile: { name: 'Test' } });
    Meteor.users.update({_id: id }, { $set: { admin: false }});
}

And after that, I need to insert data and link it to this account with its ID. (In different collections).

So I tried to do something like that, but obviously It didn't work:

UserData = new Mongo.Collection('user_data');

    if(!Meteor.users.findOne({emails: { $elemMatch: { address: "[email protected]"}}})){
        var id = Accounts.createUser({ email: "[email protected]", password: "1234", profile: { name: 'Test' } });
        Meteor.users.update({_id: id }, { $set: { admin: false }});
        UserData.insert({
            createdBy: id,
            firstname: "test",
            /* ... */
        });
    }

EDIT

Sorry for not have been clear.

The real issue is the :

UserData = new Mongo.Collection('user_data'); 

declaration is in another file, so I can't do like above.

As it's not in the same file, I tried to get the userId that got "[email protected]" as the email (the account's email created at startup). And once I got it, I want to use it in "createdBy: ID_HERE".

halbgut

Ok, you'll want to check out Structuring your application. You'll have to make the file with the definition load earlier, or the one with the fixture later.

Normally you have your collections inside lib/ and your fixtures inside server/fixtures.js.

So if you put your insert code into server/fixtures.js it'll work.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

What's an equivalent to Meteor.startup in Storybook

分類Dev

Meteor bundle error on startup

分類Dev

MongoDB Collection as data source for cs.js in Meteor app

分類Dev

Meteor Collection.insert()がIDを返さない

分類Dev

Collection.insert は関数ではありません - Meteor

分類Dev

Meteor Collection.allow

分類Dev

Meteor: observe a collection with reactiveVar

分類Dev

Meteor.CollectionおよびMeteor.Collection.Cursor

分類Dev

inserting value in meteor collection document

分類Dev

Updating already published collection in Meteor

分類Dev

Updating array element in a collection in Meteor

分類Dev

How to insert a blob into mongodb in meteor

分類Dev

Meteor mongo insert unique document

分類Dev

meteorは、特定のSession / Sessionsがfalseでない場合にのみ、Collection.insertを許可します

分類Dev

Profiling data for Meteor application

分類Dev

meteor not printing any data

分類Dev

Meteor drop collection after every query

分類Dev

Meteor: "TypeError: undefined is not an object" when using collection

分類Dev

How to query username from userId in a collection in Meteor?

分類Dev

Subscribe meteor collection through a handlebars helper

分類Dev

How to get the highest _id for the collection in meteor?

分類Dev

meteor.js - set controller to access collection

分類Dev

Meteor access data context in onCreated

分類Dev

Push arbitrary data to a Meteor server

分類Dev

Displaying data from Meteor 1.0

分類Dev

Meteor.js:Collection.insertはサーバーでは機能しますが、クライアントでは機能しません

分類Dev

Meteor.js:Collection.insertはサーバーでは機能しますが、クライアントでは機能しません

分類Dev

WCF service startup error "This collection already contains an address with scheme http"

分類Dev

Collection View Insert Item Weird Animation

Related 関連記事

  1. 1

    What's an equivalent to Meteor.startup in Storybook

  2. 2

    Meteor bundle error on startup

  3. 3

    MongoDB Collection as data source for cs.js in Meteor app

  4. 4

    Meteor Collection.insert()がIDを返さない

  5. 5

    Collection.insert は関数ではありません - Meteor

  6. 6

    Meteor Collection.allow

  7. 7

    Meteor: observe a collection with reactiveVar

  8. 8

    Meteor.CollectionおよびMeteor.Collection.Cursor

  9. 9

    inserting value in meteor collection document

  10. 10

    Updating already published collection in Meteor

  11. 11

    Updating array element in a collection in Meteor

  12. 12

    How to insert a blob into mongodb in meteor

  13. 13

    Meteor mongo insert unique document

  14. 14

    meteorは、特定のSession / Sessionsがfalseでない場合にのみ、Collection.insertを許可します

  15. 15

    Profiling data for Meteor application

  16. 16

    meteor not printing any data

  17. 17

    Meteor drop collection after every query

  18. 18

    Meteor: "TypeError: undefined is not an object" when using collection

  19. 19

    How to query username from userId in a collection in Meteor?

  20. 20

    Subscribe meteor collection through a handlebars helper

  21. 21

    How to get the highest _id for the collection in meteor?

  22. 22

    meteor.js - set controller to access collection

  23. 23

    Meteor access data context in onCreated

  24. 24

    Push arbitrary data to a Meteor server

  25. 25

    Displaying data from Meteor 1.0

  26. 26

    Meteor.js:Collection.insertはサーバーでは機能しますが、クライアントでは機能しません

  27. 27

    Meteor.js:Collection.insertはサーバーでは機能しますが、クライアントでは機能しません

  28. 28

    WCF service startup error "This collection already contains an address with scheme http"

  29. 29

    Collection View Insert Item Weird Animation

ホットタグ

アーカイブ