簡體   English   中英

meteor.js-將數組推送到用戶集合

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

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

}, [...]

問題

用戶方法中的兩個控制台日志產生正確的值(即,剛創建的事件和當前用戶的值)。 但是,我無法將這些添加到用戶集合。 通過控制台和終端(流星蒙哥)查看它可以發現該字段尚未填寫。 另外,永遠不會調用addUserEvent方法中的console.log ,因此那里可能存在問題。

您正在客戶端調用兩個方法。 它們被異步調用,因此當第一個調用仍在執行時,第二個調用已被觸發。 這就是為什么您有回調。 為了修復您的代碼,請在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));

UPDATE

對於其他有類似問題的人,問題是您需要使用$push而不是$set 這是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