【问题标题】:meteor.js - pushing array to user collectionmeteor.js - 将数组推送到用户集合
【发布时间】: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


    【解决方案1】:

    更新

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

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

    【讨论】:

      【解决方案2】:

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

      类似这样的:

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

      【讨论】:

      • 谢谢 - 我刚刚尝试了这种方法,但仍然没有看到用户方法中的 console.log,也没有看到添加到我的用户集合中的字段。有什么建议吗?
      • 但是触发了“点击#mainEventType”事件?我建议在'click #mainEventType' : function(event) { 之后添加额外的日志行,以查看在到达Meteor.call('addEvent' 之前是否有任何问题。并在浏览器的调试器中检查未定义的变量。
      • 其实我只是看了一下终端,用户方法console.log毕竟是注销了。但是,该字段/值仍未添加到集合中。这可能与我试图将其插入主用户集合而不是 user.profile 的事实有关吗?
      • 最终解决了这个问题 - 感谢您的帮助并让我知道 bind(this) - 这在我工作的其他部分非常有用。
      猜你喜欢
      • 2018-09-23
      • 2017-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 2016-04-09
      相关资源
      最近更新 更多