简体   繁体   English

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

[英]meteor.js - pushing array to user collection

CONTEXT CONTEXT

I've just created a new user and now want to push a value on to an array within my user collection. 我刚刚创建了一个新用户,现在想将一个值推入我的用户集合中的数组。 I'm using the following codes: 我正在使用以下代码:

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

}, [...]

PROBLEM 问题

The two console logs in the User Method yield the correct values (ie of the event that was just created and of the current user). 用户方法中的两个控制台日志产生正确的值(即,刚创建的事件和当前用户的值)。 However, I am unable to add these to the user collection. 但是,我无法将这些添加到用户集合。 Looking at it through both console and terminal (meteor mongo) reveals that the field hasn't been filled. 通过控制台和终端(流星蒙哥)查看它可以发现该字段尚未填写。 Also, the console.log 's in the addUserEvent method are never being called, so maybe there is a problem there. 另外,永远不会调用addUserEvent方法中的console.log ,因此那里可能存在问题。

You are calling two methods client side. 您正在客户端调用两个方法。 They are called asynchronously so the second call is already fired when the first is still executing. 它们被异步调用,因此当第一个调用仍在执行时,第二个调用已被触发。 That's why you have callbacks. 这就是为什么您有回调。 In order to fix your code, have the second call to addUserEvent inside the callback of addEvent . 为了修复您的代码,请在addEvent的回调内第二次调用addUserEvent And check error before calling addUserEvent . 并在调用addUserEvent之前检查error

Something like this: 像这样:

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

});

BTW, if you want access to this , add .bind(this) to the callback like this: 顺便说一句,如果您想访问this ,请将.bind(this)添加到回调中,如下所示:

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

UPDATE UPDATE

For anyone else having a similar problem, the issue was that you need to use $push instead of $set . 对于其他有类似问题的人,问题是您需要使用$push而不是$set This is what the push function should look like: 这是push函数的外观:

'addUserEvent': function (organizerId, params) {

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM