Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何在另一个承诺中返回一个承诺的价值?_Angular_Typescript_Firebase_Google Cloud Firestore_Ionic4 - Fatal编程技术网

Angular 如何在另一个承诺中返回一个承诺的价值?

Angular 如何在另一个承诺中返回一个承诺的价值?,angular,typescript,firebase,google-cloud-firestore,ionic4,Angular,Typescript,Firebase,Google Cloud Firestore,Ionic4,我试图在一个承诺中返回一个承诺函数的值。仍然是相当新的承诺,所以任何建议是非常感谢 // card.service.ts getCard(cardId: string): DocumentData { return firebase.firestore().collection('users').doc(this.currentUserId) .collection('cardsList').doc(cardId) .get() .the

我试图在一个承诺中返回一个承诺函数的值。仍然是相当新的承诺,所以任何建议是非常感谢

// card.service.ts

getCard(cardId: string): DocumentData {
    return firebase.firestore().collection('users').doc(this.currentUserId)
        .collection('cardsList').doc(cardId)
        .get()
        .then((docSnapShot) => {
            console.log((docSnapShot));
            return docSnapShot.data();
        },
        (err) => {
            console.log(err);
        });
  }

addCardToWallet(userId: string, cardId: string, dataObject: DocumentData) {
      return firebase.firestore().collection('users').doc(userId)
          .collection('walletList')
          .doc(cardId)
          .set(
              dataObject
          );
  }

// scanner.component.ts
scanCode() {
   this.barcodeScanner.scan(
          {
           showFlipCameraButton : true, // iOS and Android
           showTorchButton : true, // iOS and Android
           prompt : 'Scan a Kardbox QR' // Android
          }
     ).then((barcodeData) => {
         this.cardService.getCard(barcodeData.text)
             .then((data) => {
               // I want to add this to the database first

               this.cardService.addCardToWallet(this.currentUserId, barcodeData.text, data);

              // After adding values to database, navigate to other page

              this.router.navigate(['/home/wallet']);
        });
     });
 }

我得到的输出是barcodeData的值,我不知道它为什么这样做。

我认为您应该尝试异步等待,替换:

this.barcodeScanner.scan(
        {
          showFlipCameraButton : true, // iOS and Android
          showTorchButton : true, // iOS and Android
          prompt : 'Scan a Kardbox QR' // Android
        }
    ).then((barcodeData) => {
        this.cardService.getCard(barcodeData.text)
            .then((data) => {
              // I want to add this to the database first
          this.cardService.addCardToWallet(this.currentUserId, barcodeData.text, data);

          // After adding values to database, navigate to other page

          this.router.navigate(['/home/wallet']);
    });
});
与:


不要忘记在scancode()方法之前添加async。

我认为您应该尝试async wait,replace:

this.barcodeScanner.scan(
        {
          showFlipCameraButton : true, // iOS and Android
          showTorchButton : true, // iOS and Android
          prompt : 'Scan a Kardbox QR' // Android
        }
    ).then((barcodeData) => {
        this.cardService.getCard(barcodeData.text)
            .then((data) => {
              // I want to add this to the database first
          this.cardService.addCardToWallet(this.currentUserId, barcodeData.text, data);

          // After adding values to database, navigate to other page

          this.router.navigate(['/home/wallet']);
    });
});
与:


不要忘记在scancode()方法之前添加async。

您应该在getCard(cardd:string)函数中返回一个承诺


您应该在getCard(cardd:string)函数中返回一个承诺


也许这会有帮助。如果没有,请告诉我

scanCode(){
这个是.barcodeScanner.scan({
showFlipCameraButton:没错,
showTorchButton:正确,
提示:“扫描Kardbox QR”
})
.然后((条形码数据)=>{
this.cardService.getCard(barcodeData.text)
。然后((数据)=>{
//addCardToWallet返回一个承诺,对吗?等待它返回
this.cardService.addCardToWallet(this.currentUserId,barcodeData.text,data)
.然后(()=>{
//添加卡后,告诉路由器导航
这个.router.navigate(['/home/wallet']);
})
});
})
//以防万一,我建议您抓住承诺链中可能出现的任何错误
.catch((错误)=>{
console.error('Oops,有东西失败了:“+error+”);
});
}

也许这会有所帮助。如果没有,请告诉我

scanCode(){
这个是.barcodeScanner.scan({
showFlipCameraButton:没错,
showTorchButton:正确,
提示:“扫描Kardbox QR”
})
.然后((条形码数据)=>{
this.cardService.getCard(barcodeData.text)
。然后((数据)=>{
//addCardToWallet返回一个承诺,对吗?等待它返回
this.cardService.addCardToWallet(this.currentUserId,barcodeData.text,data)
.然后(()=>{
//添加卡后,告诉路由器导航
这个.router.navigate(['/home/wallet']);
})
});
})
//以防万一,我建议您抓住承诺链中可能出现的任何错误
.catch((错误)=>{
console.error('Oops,有东西失败了:“+error+”);
});
}

Hi Msu Arven。谢谢你的回答。我测试了你的解决方案,但不幸的是,它不起作用。没有向数据库中添加任何内容,也没有将我重定向到另一个页面。添加try-catch并将错误记录到控制台以查看问题所在。它表示此.cardService.getCard promise中的“数据”未定义。如果您在try-catch块中初始化常量,则该常量不能在此try-catch范围之外使用,因此,将addCardToWallet和navigate方法放在try catchHi Msu Arven中。谢谢你的回答。我测试了你的解决方案,但不幸的是,它不起作用。没有向数据库中添加任何内容,也没有将我重定向到另一个页面。添加try-catch并将错误记录到控制台以查看问题所在。它表示此.cardService.getCard promise中的“数据”未定义。如果您在try-catch块中初始化常量,则该常量不能在此try-catch范围之外使用,所以把addCardToWallet和navigate方法放在try Catch中谢谢你回答Pato。错误是this.cardService.getCard承诺中的“数据”未定义。你能告诉我吗?谢谢你回答帕托。错误是this.cardService.getCard承诺中的“数据”未定义。你能告诉我吗?嗨,拉妮卡,非常感谢你的回答。不幸的是,我也得到了与belowHi Ranika提供的其他解决方案相同的错误,非常感谢您的回答。不幸的是,我也得到了与下面提供的其他解决方案相同的错误。你说我得到的输出是什么意思?该输出在调用
scanCode()
方法的方法中?没错!您希望从
scanCode()
返回什么结果?我得到的输出是什么意思?该输出在调用
scanCode()
方法的方法中?没错!您希望从
scanCode()
返回什么结果?
getCard(cardId: string): Promise<DataType or any> {
    const promise = new Promise((resolve,reject) => { 
         firebase.firestore().collection('users').doc(this.currentUserId)
        .collection('cardsList').doc(cardId)
        .get()
        .then((docSnapShot) => {
            console.log((docSnapShot));
            resolve(docSnapShot.data());
        },
        (err) => {
            console.log(err);
        });
    });
    return promise;
  }
this.cardService.addCardToWallet(this.currentUserId, barcodeData.text,data).then(res => {
      router.navigate(/somwhere)
});