meteor.js - 将数组推送到用户集合

时间:2015-12-16 21:47:00

标签: javascript arrays node.js meteor meteor-accounts

CONTEXT

我刚刚创建了一个新用户,现在想要将值推送到我的用户集合中的数组。我使用以下代码:

//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永远不会被调用,因此可能存在问题。

2 个答案:

答案 0 :(得分:1)

您正在调用两个方法客户端。它们被异步调用,因此当第一个调用仍在执行时,第二个调用已被触发。这就是你回调的原因。为了修复您的代码,请在addUserEvent的回调中第二次调用addEvent。 在致电error之前检查addUserEvent

这样的事情:

//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"));

    if (!error) {
        eventId = [] 
        eventId.push(Session.get("myEventId"))
       //set params for user   
       var userParams = {
           userEvents: eventId
       }

       console.log(userParams)

       Meteor.call('addUserEvent', Session.get("organizerId"), userParams);
    }

});

顺便说一句,如果您想要访问this,请将.bind(this)添加到回调中,如下所示:

Meteor.call('addEvent', eventParams, function(error, result){
   // Your callback function body
}.bind(this));

答案 1 :(得分:0)

<强>更新

对于遇到类似问题的其他人,问题是您需要使用$push而不是$set。这就是推送功能应该是这样的:

'addUserEvent': function (organizerId, params) {

Meteor.users.update({
    _id: organizerId
    } , { 
    $push: params
 });

}