Angular5:在我的组件中恢复承诺状态服务

Angular5 : recover promise statut service in my component

这一点我头发都掉了。 我使用:Angular5 cli + AngularFire2

1° 在我的组件中,我的 函数 destroyUnicorn 调用我的 service updateUnicorn1

2° 因为我想知道请求是否有效,所以我在我的服务 updateUnicorn1 中做了一个 Promise

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');
    });
};

我的service.ts

    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); 
        }); 
    }); 
  }

我认为问题出在 return。再见