【发布时间】:2016-03-23 05:16:28
【问题描述】:
上下文
我刚刚创建了一个新用户,现在想将一个值推送到我的用户集合中的一个数组中。我正在使用以下代码:
//User Schema - in User Collection
Schema.User = new SimpleSchema({
userEvents: {
type: [String],
optional: true
}, [...]
//User Methods - in server/methods
Meteor.methods({
'addUserEvent': function (organizerId, params) {
console.log(organizerId);
console.log(params);
Meteor.users.update({
organizerId: organizerId
},{
$set:params
});
}
});
//Create new event and add ID of event to current user - in events controller
EventsController.events({
'click #mainEventType' : function(event) {
var organizerId = Accounts.userId();
var eventStatus = "processStarted";
//get value from selection
var mainEventType = event.target.alt;
var eventParams = {
organizerId: organizerId,
mainEventType: mainEventType,
eventStatus: eventStatus
}
//Insert Event and use callback to get the id of the even you just inserted
Meteor.call('addEvent', eventParams, function(error, result){
//use session set to store value of user id and event id
Session.set("organizerId", Accounts.userId());
Session.set("myEventId", result)
console.log("organizer ID: " + Session.get("organizerId"));
console.log("usereventId: " + Session.get("myEventId"));
});
eventId = []
eventId.push(Session.get("myEventId"))
//set params for user
var userParams = {
userEvents: eventId
}
console.log(userParams)
Meteor.call('addUserEvent', Session.get("organizerId"), userParams);
}, [...]
问题
用户方法中的两个控制台日志产生正确的值(即刚刚创建的事件和当前用户的值)。但是,我无法将这些添加到用户集合中。通过控制台和终端(meteor mongo)查看它表明该字段尚未填充。另外,addUserEvent 方法中的console.log 永远不会被调用,所以可能存在问题。
【问题讨论】:
标签: javascript arrays node.js meteor meteor-accounts