在Meteor启动时将数据插入集合中 [英] Insert data in collection at Meteor's startup
问题描述
我想在Meteor的启动时插入数据. (然后是JSON文件)
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: "test@test.com"}}})){
var id = Accounts.createUser({ email: "test@test.com", password: "1234", profile: { name: 'Test' } });
Meteor.users.update({_id: id }, { $set: { admin: false }});
}
然后,我需要插入数据并将其链接到具有其ID的该帐户. (在不同的集合中).
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: "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",
/* ... */
});
}
编辑
对不起,不清楚.
真正的问题是:
UserData = new Mongo.Collection('user_data');
声明在另一个文件中,所以我不能像上面一样.
declaration is in another file, so I can't do like above.
因为它不在同一个文件中,所以我尝试获取以"test@test.com"作为电子邮件地址(在启动时创建的帐户电子邮件地址)的userId.而一旦获得它,我想在"createdBy:ID_HERE"中使用它.
As it's not in the same file, I tried to get the userId that got "test@test.com" as the email (the account's email created at startup). And once I got it, I want to use it in "createdBy: ID_HERE".
推荐答案
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.
通常,您的集合位于 lib/中,而fixture
位于 server/fixtures.js 中.
Normally you have your collections inside lib/ and your fixture
s inside server/fixtures.js.
因此,如果将插入代码放入 server/fixtures.js 中,它将起作用.
So if you put your insert code into server/fixtures.js it'll work.
这篇关于在Meteor启动时将数据插入集合中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!