我正在尝试使用帖子做一个夹具数据库,这些帖子将属于创建的用户......两个集合都将在Meteor.startup上创建,所以我需要知道用户ID才能将它们添加到发布并建立他们之间的关系,但我不知道如何使用自定义ID创建用户,以便我可以连接到帖子...这是我的代码:
文章:
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
});
用户
if ( Meteor.users.find().count() < 100 ) {
Accounts.createUser(
{
_id: "aWcGJaqXeM64uGE9M",
username: "Davenport",
emails: "Davenport@gmail.com",
password: "Mcdowell"
},
);
});
您可以看到我创建用户并尝试向其添加特定ID但是当我运行应用时,ID字段不会被考虑在内并且会将其覆盖为全新的用户ID
答案 0 :(得分:2)
Accounts.createUser将返回新创建的用户的ID。
所以你可以这样做:
var userId = Accounts.createUser({
username: "Davenport",
emails: "Davenport@gmail.com",
password: "Mcdowell"
});
Posts.insert({
createdAt: new Date(),
postStatus: "lorem ipsum blah blah",
type: "post",
authorId: userId
});