2015-12-16 55 views
1

背景meteor.js - 推陣列用戶採集

我剛剛創建了一個新的用戶,現在想上推值我的用戶集合中的數組。我使用以下代碼:

//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永遠不會被調用,所以可能在那裏有問題。

回答

1

您正在調用客戶端的兩種方法。它們被異步調用,所以第二個調用在第一個調用仍在執行時已經被觸發。這就是爲什麼你有回調。爲了修復您的代碼,請在addEvent的回調中撥打addUserEvent。 並檢查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)); 
+0

謝謝你 - 我只是嘗試這種做法,我還沒有看到不從用戶的方法'console.log'的,或實地添加到我的用戶採集。有什麼建議麼? – Sekoul

+1

但是事件'click #mainEventType'被觸發?在''點擊#mainEventType'之後,我會建議額外的日誌行:function(event){'在它到達Meteor.call('addEvent')之前查看是否有任何錯誤並查看瀏覽器的調試器來檢查undefined變量 –

+0

實際上,我只是看着終端和用戶方法console.log畢竟是註銷,但字段/值仍然沒有被添加到集合中,這可能與我的事實有關'試圖插入主要用戶集合而不是user.profile? – Sekoul

0

UPDATE

對於任何人有類似的問題,問題是你需要使用$push代替$set。這是推送功能應該是什麼樣子:

'addUserEvent': function (organizerId, params) { 

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

}