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.
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
. And check error
before calling addUserEvent
.
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:
Meteor.call('addEvent', eventParams, function(error, result){
// Your callback function body
}.bind(this));
UPDATE
For anyone else having a similar problem, the issue was that you need to use $push
instead of $set
. This is what the push function should look like:
'addUserEvent': function (organizerId, params) {
Meteor.users.update({
_id: organizerId
} , {
$push: params
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.