gpt4 book ai didi

javascript - 未处理的 promise 拒绝 : Cannot call method 'push' of null

转载 作者:搜寻专家 更新时间:2023-10-30 21:12:40 24 4
gpt4 key购买 nike

我有一个我似乎无法理解的问题。如果有人可以提供一些建议,我将不胜感激。

我正在使用 Ionic2 和 Meteor/Mongo 构建一个聊天应用程序。

我有以下代码:

  private findChats(): Promise<Mongo.Collection<Chat>> {
let promise: Promise<Mongo.Collection<Chat>> = new Promise<Mongo.Collection<Chat>>(resolve => {
let registeredIds: String[] = [this.senderId];
for (let i = 0; i < this.jobModelsForSender.length; i++) {
console.log('findChats: registeredIds.push this.jobModelsForSender[i].id: ' + registeredIds + ' - ' + this.jobModelsForSender[i].id);
registeredIds.push('J' + this.jobModelsForSender[i].id);
console.log('findChats: pushed: ' + registeredIds);
}
this.subscribe('chats', this.senderId, registeredIds, () => {
console.log('findChats: in subscribe: ' + this.senderId+' '+registeredIds);

let chats: Mongo.Cursor<Chat> = Chats.find(
{ memberIds: { $in: registeredIds } },
{
sort: { lastMessageCreatedAt: -1 },
transform: this.transformChat.bind(this),
fields: { memberIds: 1, lastMessageCreatedAt: 1 }
}
);
console.log('findChats: chats: ' + chats);
this.chats = chats;
let localChatCollection: Mongo.Collection<Chat> = new Mongo.Collection<Chat>(null);
chats.forEach((chat: Chat) => {
localChatCollection.insert(chat);
});
console.log('findChats: resolve(localChatCollection): ' + localChatCollection);
resolve(localChatCollection);
});
});
return promise;
}

但我得到以下输出:

findChats:  registeredIds.push this.jobModelsForSender[i].id: P8  -  72 app.bundle.js:118232
findChats: pushed: P8,J72 app.bundle.js:118234
findChats: registeredIds.push this.jobModelsForSender[i].id: P8 - 72 app.bundle.js:118232
findChats: pushed: P8,J72 app.bundle.js:118234
findChats: registeredIds.push this.jobModelsForSender[i].id: P8 - 72 app.bundle.js:118232
findChats: pushed: P8,J72 app.bundle.js:118234
Unhandled Promise rejection: Cannot call method 'push' of null ; Zone: <root> ; Task: Promise.then ; Value: TypeError {stack: (...), message: "Cannot call method 'push' of null"} zone.js:461
Error {rejection: TypeError, promise: ZoneAwarePromise, zone: Zone, task: ZoneTask, stack: (...)…}
zone.js:463
Unhandled Promise rejection: Cannot call method 'push' of null ; Zone: <root> ; Task: Promise.then ; Value: TypeError {stack: (...), message: "Cannot call method 'push' of null"} zone.js:461
Error {rejection: TypeError, promise: ZoneAwarePromise, zone: Zone, task: ZoneTask, stack: (...)…}
zone.js:463
Unhandled Promise rejection: Cannot call method 'push' of null ; Zone: <root> ; Task: Promise.then ; Value: TypeError {stack: (...), message: "Cannot call method 'push' of null"} zone.js:461
Error {rejection: TypeError, promise: ZoneAwarePromise, zone: Zone, task: ZoneTask, stack: (...)…}
zone.js:463
findChats: in subscribe: P8 P8,J72 app.bundle.js:118243
findChats: chats: [object Object] app.bundle.js:118249
findChats: resolve(localChatCollection): [object Object]

如您所见,我在 zone.js 中遇到错误。据我所知,当我调试它时,如日志所示,它成功地将 push 发送到 registeredIds。然后它退出函数,返回 promise。之后它进入记录错误的 zone.js

最佳答案

答案可能与很多人无关,但正如安东指出的那样,这是一个与异步加载 promise 有关的问题。

我有一个事件同时调用同一个 promise 。一旦我删除该事件的触发器,我就不会收到任何错误。

关于javascript - 未处理的 promise 拒绝 : Cannot call method 'push' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39890210/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com