Inserting data into a collection when starting Meteor

I would like to insert data when Meteor starts. (And after from a JSON file)

On startup, I create a new account, and I would like to insert data and associate it with this account after creating this.

This is the code that creates a new account on startup:

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

      

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

So I tried to do something like this, but obviously it didn't work:

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

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

      

EDIT

Sorry it wasn't clear.

The real problem:

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

      

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

As it is not in the same file, I tried to get the userId that I received " test@test.com " as an email (the account email generated at startup). And once I got it, I want to use it in "createdBy: ID_HERE".

+3


source to share


1 answer


Ok, you will want to check out Structuring Your Application . You will have to make a file with a load of definitions sooner or later with fixture

.

Usually you have your collections inside lib / and yours fixture

inside the server /fixtures.js.



So, if you paste the embed code into the /fixtures.js server, it will work.

+2


source







All Articles