简体   繁体   中英

TypeError: 'undefined' is not an object (evaluating 'promise.data.map')

I'm currently creating unit tests for a project implemented in angular ver. 1.2
I've created a mockService returning a deferred promise to use with my controller tests. A service method looks like this:

function getItems() {
    var deferred = $q.defer();
    deferred.resolve(["item1", "item2"]);
    $rootScope.$apply();

    return deferred.promise;

}

When I call this service method and deferred.resolve is executed I get an error

TypeError: 'undefined' is not an object (evaluating 'promise.data.map')

Does anyone know what the promise.data.map is and what I need to do to fix the error?

Here is the solution to your problem

In your question you are trying to call the promise before the value of the rootScope that have been defined in your controller which is causing the problem. Hence it is returning the undefined value.

Try to define the promise after the rootScope values in your controller ,which may fix the error message

Hope this solution may help you a bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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