我有一个Javascript问题我有点困惑。我有一个ts文件,包含以下内容:
private localMessageCollection: Mongo.Collection<Message> = new Mongo.Collection<Message>(null);
...
let that = this;
this.subscribe('messages', this.activeChat._id, this.senderId, () => {
this.autorun(() => {
let promiseMessages: Promise<Mongo.Collection<Message>> = this.findMessages();
promiseMessages.then((messageData: Mongo.Collection<Message>) => {
console.log('find messageData: ' + messageData);
messageData.find().forEach(function (message: Message) {
console.log('find that.localMessageCollection.insert(message): ' + message);
that.localMessageCollection.insert(message);
console.log('done!!!');
});
});
});
});
我的问题是以下行没有执行。我希望&#39; done!!!
&#39;打印。
that.localMessageCollection.insert(message);
输出
find messageData: [object Object] find that.localMessageCollection.insert(message): [object Object]
它与我认为的对象范围有关,因为如果我在它上面添加以下行,它就会运行。
let localMessageCollection: Mongo.Collection<Message> = new Mongo.Collection<Message>(null);
localMessageCollection.insert(message);
任何建议表示赞赏。
已编译代码:
this.localMessageCollection = new mongo_1.Mongo.Collection(null);
....
MessagesPage.prototype.loadMessages = function () {
var _this = this;
var promise = this.utilityService.getLoggedInPerson();
if (promise) {
promise.then(function (data) {
_this.personModelLoggedIn = JSON.parse(data);
if (_this.personModelLoggedIn) {
_this.senderId = 'P' + _this.personModelLoggedIn.id;
_this.title = _this.activeChat.title;
_this.picture = _this.activeChat.picture;
var that_1 = _this;
_this.subscribe('messages', _this.activeChat._id, _this.senderId, function () {
_this.autorun(function () {
var promiseLocalMessages = _this.findLocalMessages();
promiseLocalMessages.then(function (messageData) {
console.log('local messageData: ' + messageData);
messageData.find().forEach(function (message) {
console.log('local that.localMessageCollection.insert(message): ' + message);
that_1.localMessageCollection.insert(message);
});
});
var promiseMessages = _this.findMessages();
promiseMessages.then(function (messageData) {
console.log('find messageData: ' + messageData);
messageData.find().forEach(function (message) {
console.log('find that.localMessageCollection.insert(message): ' + message);
//console.log(that.localMessageCollection);
that_1.localMessageCollection.insert(message);
//console.log(that.localMessageCollection);
});
});
});
});
}
else {
_this.nav.push(login_1.LoginPage, {
fromMessages: true
});
}
});
}
else {
this.nav.push(login_1.LoginPage, {
fromMessages: true
});
}
};
更新
我尝试实施以下人员的建议,但我必须做错事。如果有人可以帮助我,我将不胜感激。
我尝试以下方法:
this.subscribe('messages', this.activeChat._id, this.senderId, () => {
this.autorun(() => {
let promiseMessages: Promise<Mongo.Collection<Message>> = this.findMessages();
promiseMessages.then((messageData: Mongo.Collection<Message>) => {
messageData.find().forEach(function (message: Message) {
setLocalMessage.bind(message);
});
});
});
和
private setLocalMessage(message: Message): void {
this.localMessageCollection.insert(message);
}
当我尝试构建应用时出现以下错误:
ERROR in ./app/pages/messages/messages.ts (72,19): error TS2304: Cannot find name 'setLocalNessage'.