简体   繁体   English

异步服务调用不起作用

[英]Async service call not working

here is my index.js 这是我的index.js

import ProductDetailController from './product-detail.controller.js';
import ProductDetailManager from './product-detail.manager.js';
import ProductDetailService from './../services/product-detail.service.js';

export default angular.module('myApp.productDetail', [uirouter])
    .controller('ProductDetailController', ProductDetailController)
    .service('productDetailManager', ProductDetailManager)
    .service('productDetailService', ProductDetailService)        
    .name;

here is my controller 这是我的控制器

export default class ProductDetailController {
    constructor( logger, productDetailManager, $stateParams) {

        this.productDetailManager = productDetailManager;            
        this.activate();
    }

    activate() {
        return this.loadProduct(this.$stateParams.boutiqueId,this.$stateParams.productId).then(()=> {

        });
    }

    loadProduct(boutiqueId, productId) {
        //async
        return this.productDetailManager.getProduct(boutiqueId, productId).then((response)=> {
            return this.response;
        });
    }
}

ProductDetailController.$inject = [ 'logger', 'productDetailManager', '$stateParams'];

Here is my Manager 这是我的经理

export default class ProductDetailManager {
    constructor( logger, productDetailService) {

        this.productDetailService = productDetailService;
        this.logger = logger;

    }

    getProduct(boutiqueId, productId) {
        //async
        return this.productDetailService.find(boutiqueId, productId).then((response)=> {
            console.log(this.response);
            return this.response;
        });
    }
}

ProductDetailManager.$inject = [ 'logger', 'productDetailService'];

And Here is my service 这是我的服务

export default class ProductDetailService {

    constructor($http, $q, logger, configurationService) {
        this.$http = $http;
        this.$q = $q;
        this.logger = logger;
        this.configurationService = configurationService;
        this.basePath = this.configurationService.get('catalogApiBasePath');
    }


    find(boutiqueId, productId) {


        var deferred = this.$q.defer();

        this.$http.defaults.headers.common.Authorization = 'Bearer ---';
        this.$http.defaults.headers.common.X_AUTH_VERSION = '---';

        this.$http.get(`${this.basePath}/product/detail?boutiqueId=${boutiqueId}&productId=${productId}`)

            .success((data, status, headers, config)=> {
                deferred.resolve(data);
            })
            .error((data, status) => {
                this.logger.error('XHR Failed for ProductDetailService#find.' + data);
                deferred.reject(data);
            });

        return deferred.promise;
    }
}

ProductDetailService.$inject = ['$http', '$q', 'logger', 'configurationService'];

I wanted to change service data on my manager then return to the controller, however manager is getting undefined data. 我想在经理上更改服务数据,然后返回到控制器,但是经理正在获取未定义的数据。

I am doing something wrong with async calls and could not find what I am missing here 我在异步调用中做错了什么,在这里找不到我想念的东西

I guess the error is here: 我猜错误在这里:

 return this.productDetailService.find(boutiqueId, productId).then((response) => {
     console.log(this.response); // here should be just `response` as it is argument
     return this.response;
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM