角度类型错误:无法读取未定义的属性。

时间:2022-11-26 18:51:32

I have a data service like this:

我有这样的数据服务:

this.myFunction= function(callback) {
    var url = rootURL + "path1/path2/service.json";
    var promise = $http.get(url);
    promise.then(function(payload){
        return callback(payload); 
    });
    return promise; 
}

It is called in a controller to initialize some stuff:

在控制器中调用它来初始化一些东西:

DataService.myFunction(function(data) { 
    if(data.statusText !== "OK"){
        $scope.$worked= false; 
    }else{  
        $scope.$worked= true;               
    }
}

And I get "TypeError: Cannot read property 'then' of undefined". Console.log(data) in the callback shows a 200 "OK" response and the data I expect. I have searched for this error already and mostly it is due to not returning the promise in the service. However, I'm returning the promise. Setting anything on the controller scope in the callback causes the error.

我得到"TypeError:不能读取属性'then' of undefined"回调中的Console.log(data)显示了200个“OK”响应和我期望的数据。我已经搜索了这个错误,主要是因为没有返回服务中的承诺。不过,我还是会兑现承诺。在回调的控制器范围中设置任何内容都会导致错误。

Angular version: AngularJS v1.3.0-rc.2

角版:AngularJS v1.3.0-rc.2

Thanks!

谢谢!

1 个解决方案

#1


22  

You don't need to return a promise in this case, because you are using a callback. Callbacks and promises are the two ends of the spectrum. You can accomplish what you want simply with this.

在这种情况下,您不需要返回一个承诺,因为您正在使用回调。回调和承诺是两个极端。你可以用这个简单地完成你想要的。

If you want to use a callback you can leave your controller code.

如果要使用回调,可以保留控制器代码。

this.myFunction= function(callback) {
    var url = rootURL + "path1/path2/service.json";
    $http.get(url).then(function(response) {
        callback(response.data);
    });
}

Or if you want to utilize the promises

或者如果你想利用承诺

this.myFunction= function() {
    var url = rootURL + "path1/path2/service.json";
    return $http.get(url).then(function(response) {
        return response.data;
    });
}

DataService.myFunction().then(function(data) { 
    if(data.statusText !== "OK"){
        $scope.$worked = false; 
    } else {  
        $scope.$worked = true;               
    }
});

#1


22  

You don't need to return a promise in this case, because you are using a callback. Callbacks and promises are the two ends of the spectrum. You can accomplish what you want simply with this.

在这种情况下,您不需要返回一个承诺,因为您正在使用回调。回调和承诺是两个极端。你可以用这个简单地完成你想要的。

If you want to use a callback you can leave your controller code.

如果要使用回调,可以保留控制器代码。

this.myFunction= function(callback) {
    var url = rootURL + "path1/path2/service.json";
    $http.get(url).then(function(response) {
        callback(response.data);
    });
}

Or if you want to utilize the promises

或者如果你想利用承诺

this.myFunction= function() {
    var url = rootURL + "path1/path2/service.json";
    return $http.get(url).then(function(response) {
        return response.data;
    });
}

DataService.myFunction().then(function(data) { 
    if(data.statusText !== "OK"){
        $scope.$worked = false; 
    } else {  
        $scope.$worked = true;               
    }
});