ค้นหาเว็บไซต์

javascript - การปฏิเสธ Promise ที่ไม่ได้จัดการ: ไม่สามารถเรียกใช้เมธอด 'push' ของ null


ฉันมีปัญหาที่ฉันดูเหมือนจะไม่เข้าใจ หากใครสามารถให้คำแนะนำได้ ฉันจะขอบคุณมาก

ฉันกำลังใช้ 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 ซึ่งข้อผิดพลาดถูกบันทึก

คำตอบ #1

คำตอบอาจจะไม่เกี่ยวข้องกับคนหลาย ๆ คน แต่ตามที่แอนตอนชี้ให้เห็น มันเป็นปัญหาที่เกี่ยวกับการโหลดสัญญาแบบอะซิงโครนัส

ฉันมีเหตุการณ์ที่เรียกพรอมิสเดียวกันพร้อมกัน เมื่อฉันลบตัวกระตุ้นของเหตุการณ์นั้นออก ฉันก็ไม่พบข้อผิดพลาดใด ๆ