【发布时间】:2015-03-21 00:40:37
【问题描述】:
在创建新用户时,我需要创建一些通过用户 ID 引用用户的新文档
Accounts.onCreateUser(function(options, user) {
console.log(user);
var google = user.services.google;
var tokens = {
accessToken: google.accessToken,
// refreshToken: google.refreshToken
};
if (tokens.accessToken) {
Meteor.call('createChannel', user._id, tokens, function(error, result) {
if (error) {
throw error;
}
});
}
return user;
});
在 createChannel 方法中,我使用传入的 id user._id 来设置一些引用用户文档的文档。
createChannel: function(userId, tokens) {
console.log(userId);
var missionId = Meteor.call('createDefaultMission', userId);
...
/* Creates a new mission with default values
*/
createDefaultMission: function(userId) {
doc = {
_id: Random.id(),
userId: userId,
name: 'My Mission',
};
var missionId = Missions.insert(doc);
return missionId;
},
问题是,在这种情况下,任务记录在创建后没有任何 userId 字段。使用 SimpleSchema 时,我收到 User is required 错误。
知道这是为什么吗?
更新
createDefaultMission: function(userId) {
doc = {
_id: Random.id(),
userId: userId,
name: 'My Mission',
};
console.log(doc); <----- this logs the above with the userId field filled
console.log(Meteor.users.findOne(userId)); <----- this finds the user with the above ID
var missionId = Missions.insert(doc); <---- this failed, userId field is not present
return missionId;
},
【问题讨论】:
标签: meteor