当我调用Meteor.setTimeout()时,meteor保存当前的'this'实例,当我调用的时候,我调用的函数使用旧的实例(当我调用Meteor.setTimeout()时保存的实例)。我怎样才能让meteor使用'this'的新实例呢?
// 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是最旧的日志(Meteor.setTimeout()调用时的日志)而不是新日志。
答案 0 :(得分:1)
那不是流星的事情。当您使用() => ...
时,当前此上下文将绑定到此功能。