Reputation: 47
When I am calling to Meteor.setTimeout() meteor saves the current instance of 'this' and when time's up the funcion I called uses the old instance of this (the instance that was saved when I call Meteor.setTimeout()). How can I cause meteor to use the new instance of 'this' instead ?
// Ending question
const questionEndToLog = () => this.questionEnd(firstQuestion._id);
Meteor.setTimeout(questionEndToLog, firstQuestion.time * 1000);
questionEnd(qId) {
const addToGameLog = () => {
const questionEnd = new QuestionEnd({
questionId: qId
});
this.gameLog = this.gameLog.concat(questionEnd);
this.save();
return true;
};
const isQuestionEndAlready = !!this.gameLog
.filter(e => e.nameType === eventTypes.QuestionEnd)
.find(e => e.questionId === qId);
return isQuestionEndAlready && addToGameLog();
}
export const QuestionEnd = Class.create({
name: eventTypes.QuestionEnd,
fields: {
nameType: {
type: String,
default() {
return eventTypes.QuestionEnd;
},
},
questionId: {
type: String,
},
createdAt: {
type: Date,
default() {
return new Date();
},
},
},
});
this.gameLog is the oldest log (the log that was when the Meteor.setTimeout() called) instead of the new one.
Upvotes: 0
Views: 46
Reputation: 10076
That's not a Meteor thing. When you're using () => ...
then the current this context will be bound to this function.
Upvotes: 1