我正在尝试用帖子创建一个固定数据库,这些帖子将属于创建的用户......这两个集合都将在 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: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2b6f4a5d4e455b44595f6b4c464a424705484446" rel="noreferrer noopener nofollow">[email protected]</a>",
password: "Mcdowell"
},
);
});
您可以看到我创建了用户,并尝试向其中添加特定 ID,但当我运行应用程序时,不会考虑 ID 字段,并且会将其覆盖为全新的用户 ID
最佳答案
Accounts.createUser 将返回新创建用户的 ID。
所以你可以这样做:
var userId = Accounts.createUser({
username: "Davenport",
emails: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6e2c7d0c3c8d6c9d4d2e6c1cbc7cfca88c5c9cb" rel="noreferrer noopener nofollow">[email protected]</a>",
password: "Mcdowell"
});
Posts.insert({
createdAt: new Date(),
postStatus: "lorem ipsum blah blah",
type: "post",
authorId: userId
});
关于javascript - 如何在 Meteor.startup 上为固定数据库的用户添加预定义 _id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35026812/