簡體   English   中英

Angular5:在我的組件中恢復Promise Statut服務

[英]Angular5 : recover promise statut service in my component

在這一點上,我失去了所有的頭發。 我使用:Angular5 CLI + AngularFire2

1°在我的組件中,我的函數destroyUnicorn調用我的服務updateUnicorn1

2°我想知道請求是否有效,所以我在服務updateUnicorn1中做了一個承諾

3°如果請求解決或拒絕了omg ^^,我將無法收聽。 如果請求不起作用,我想顯示console.log(“ error”)並在我的組件中執行另一個函數,另一方面, 我想顯示console.log(“ success”)並在我的組件中執行另一個函數 。 。

(但是console.log在該服務中有效)

我的Component.ts:

destroyUnicorn(item){
    this.itemService.updateUnicorn1( {
        statut: "destroyed",
        id: item.id
    })
    .then(function() {
         console.log('success');

    }).catch(function(something) {
         console.log('error');
    });
};

我的服務

    updateUnicorn1(item) {
          let self = this;

          let promise = new Promise((resolve, reject) => {
            self.itemDoc = self.afs.doc(`unicorns/${item.id}`);
            self.itemDoc.update(item)
            .then(resolve => {
                   console.log('all good');
            })
            .catch(reject => {
                  console.log('catch');
            });

        });  
       return promise;   
      }

這是我的男孩!

首先,您的組件很棒: Component.ts

destroyUnicorn(item){
    this.itemService.updateUnicorn1( {
        statut: "destroyed",
        id: item.id
    })
    .then(function() {
         console.log('success');

    }).catch(function(something) {
         console.log('error');
    });
};

第二:問題是承諾結構:所以讓我們這樣做:

updateUnicorn1(item) {
      var self = this;

      return new Promise((resolve,reject)=>{ 
        self.itemDoc = self.afs.doc(`unicorns/${item.id}`);
        self.itemDoc.update(item)
        .then(()=>{ 
            resolve({success :true}); 
        })
        .catch((err)=>{ 
            reject(err); 
        }); 
    }); 
  }

我認為問題在於回報。 再見

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM