2016-09-30 7 views
0

제품 구성 요소와 제품 소유자 구성 요소가 있습니다. 각 제품은 내가 API 엔드 포인트를 호출하여VueJS 약속이 실패 함

나는 제품 목록을 수신하고 일을하려고하고 무엇 소유자

있을 것이다. 약속이 끝나면 제품 목록이 나옵니다. 각 제품에는 OwnerID가 있습니다. 다른 API Endpoint를 호출하여 소유자의 이름을 가져오고 현재 제품 반복에 할당하려고합니다.

내 코드 지금까지 나는, 나는 다음과 같은 오류

Uncaught TypeError: Cannot read property 'then' of undefined 
Uncaught (in promise) TypeError: Promise resolver undefined is not a function(…) 

가 계속 그렇게하려고 할 때마다 나는 지금

에 직면하고있다

<script> 
    var config = require('../config'); 
    export default { 
     data() { 
      return { 
       products: [], 
      } 
     }, 
     ready() { 
      this.getProducts().then(t => { 
       console.log(t); 
      }); 
     }, 
     methods : { 
      getProducts : function() { 
       let url = config.API.GetProduct 

       this.$http.get(url).then(response=> { 
        this.products = response.data.resource; 
        var p = this.products.map(this.getOwner); 
        return Promise.all(p); 

       }, error=> { 
        console.error("An error happened!") 
       }); 
      }, 
      getOwner : function(product) { 
       let url = config.API.GetProductOwnerName.replace('[$$$]', product.OwnerID); 
       var p = new Promise(); 

       this.$http.get(url).then(response => { 
        product.OwnerID = response.data.resource[0].OwnerName; 
        p.resolve(currentObj); 
       }); 

       return p; 

      } 
     } 
     components: {} 
    } 
</script> 

오류 누군가 내가 여기서 뭘 잘못하고 있는지 알려주실 수 있습니까?

감사합니다.

답변

0

새 약속 개체를 다시 만들 필요가 없습니다. 다음 호출로 전달할 객체를 반환 할 수 있습니다.

getProducts: function() { 
    let url = config.API.GetProduct 

    return this.$http.get(url).then(response => { 
     this.products = response.data.resource; 
     return this.products.map(this.getOwner); 
    }, error=> { 
     console.error("An error happened!") 
    }); 
}, 
+0

감사합니다. 그러나 나는 여전히 같은 문제가 있습니다. – Gagan

+0

@Gagan이 나에게 잘 보입니다. https://jsfiddle.net/3q5cwuha/ – Hkan

+0

코드를 보았습니다. 그런 식으로 내 코드가 작동합니다. IMO가 약속을 통해 처리하고자하는 많은 제품을 가지고 있기 때문에 실제로하고 싶은 일. 당신의 경우, 그 매우 작은 물체 때문에, 약속은 빨리 해결되고 그러므로 당신은 메시지를 봅니다. 나는 틀릴 수도 있지만 그것은 그것이 무엇인지 생각합니다. – Gagan