AHH
AHH

Reputation: 281

How to add predefined _id to a user on Meteor.startup for a fixture DB

I am trying to do a fixture database with posts and those posts will belong to the created users....both collections will be created on Meteor.startup so I need to know the user IDs in order to add them to the posts and make a relation between them, but I don't know how to create the users with a custom ID so I can connect to the posts...here is my code:

Posts:

Meteor.startup(function () {
if ( Posts.find().count() < 100 ) {


Posts.insert(
  {
    createdAt: new Date(),
    postStatus: "lorem ipsum blah blah",
    type: "post",
    authorId: "aWcGJaqXeM64uGE9M" // this is an example of an Id i am trying to add to a user so the post get connected to that user
  });

Users

if ( Meteor.users.find().count() < 100 ) {
    Accounts.createUser(
      {
  _id: "aWcGJaqXeM64uGE9M",
  username: "Davenport",
  emails: "[email protected]",
  password: "Mcdowell"
    },
    );
});

You can see I create users and I try to add a specific ID to it but when I run the app the ID field won't be taken in consideration and it will override that to a completely new user ID

Upvotes: 2

Views: 144

Answers (1)

bluebird
bluebird

Reputation: 640

Accounts.createUser will return the id of the newly created user.

So you can do something like this:

var userId = Accounts.createUser({
  username: "Davenport",
  emails: "[email protected]",
  password: "Mcdowell"
});



Posts.insert({
    createdAt: new Date(),
    postStatus: "lorem ipsum blah blah",
    type: "post",
    authorId: userId
  });

Upvotes: 2

Related Questions