Javascript 使用 AngularJS 时出现“.then() 不是函数”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33838481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:32:16  来源:igfitidea点击:

getting ".then() is not a function" error when using AngularJS

javascriptangularjs

提问by user2719875

This is my JS:

这是我的JS:

self.obj = {}
self.obj.accessErrors = function(data) {
    var cerrorMessages = [];
    for (prop in data) {
        if (data.hasOwnProperty(prop)){
            if (data[prop] != null && data[prop].constructor ==  Object) {
                self.obj.fetch[accessErrors](data[prop]);
            }
            else {
                cerrorMessages.push(data[prop]);
            }
        }
    }
    return cerrorMessages;
};

self.obj.fetch = {
    X: function() {
        // do stuff.
    },

    Y: function(callback) {
        $http.get('/yposts/').then(
            function(response) {
                self.posts = response.data;
                callback(self.posts);
            },
            function(response) {
                self.posts = {};

                self.obj.accessErrors(response.data).then(function(cerrrorMessages) {
                    callback(posts, cerrorMessages);
                });
            }
        );
    }
};

And I am getting an error pointing to this line:

我收到指向这一行的错误:

self.obj.accessErrors(response.data).then(function(cerrrorMessages) {

The error says:

错误说:

TypeError: self.obj.accessErrors(...).then is not a function

Any idea how to solve this?

知道如何解决这个问题吗?

回答by jfriend00

self.obj.accessErrors(response.data)does not return a promise so therefore, you can't use promise methods on it.

self.obj.accessErrors(response.data)不返回承诺,因此,您不能在其上使用承诺方法。

If you want it to return a promise and you want that promise to reflect when all the fetch()operations are done and those operations are actually async, then you will have to make all your async code into using promises and you will have to combine them all using Promise.all()or the angular equivalent and convert from using callbacks in fetch to just using a promise. Right now, you have a mix which is difficult to program with.

如果您希望它返回一个承诺并且您希望该承诺反映所有fetch()操作何时完成并且这些操作实际上是异步的,那么您将必须使所有异步代码都使用承诺,并且您必须将它们全部使用Promise.all()或 angular 等价物,并从在 fetch 中使用回调转换为仅使用承诺。现在,您有一个难以编程的混合体。

回答by TheHansinator

The .then()construction is only needed when using Promiseobjects - essentially, instead of returning a value, the function returns an object that resolvesto a value at some point in the future (which is then passed into the function that you pass to .then().

.then()建筑时,才需要使用时承诺对象-本质上,而不是返回一个值,该函数返回一个对象做出决议,以在未来的某个点的值(然后传递给函数,你传递给.then()

But you are right in that you need an asynchronous pattern to do this correctly, since fetch.Yis an asynchronous method. A good thing to do would be to create an array of promises at the beginning of your accessErrorsfunction, like so:

但是您是对的,因为您需要一个异步模式才能正确执行此操作,因为它fetch.Y是一种异步方法。一件好事是在accessErrors函数的开头创建一个 promise 数组,如下所示:

var fetchPromises = [];

and then replace self.obj.fetch[accessErrors](data[prop]);with something that calls pushon that array to add the promise that fetchreturns to it.

然后替换self.obj.fetch[accessErrors](data[prop]);为调用push该数组的内容以添加fetch返回给它的承诺。

Then, instead of returning accessErrors, return Promise.all(fetchPromises).

然后,而不是返回accessErrors,返回Promise.all(fetchPromises)

This will require some fairly significant modification to your code, however - namely, you will need to rewrite it so that it uses the Promise API instead of this callback by itself (which shouldn't be too difficult to do).

然而,这将需要对您的代码进行一些相当重要的修改——也就是说,您需要重写它,以便它使用 Promise API 而不是这个回调本身(这应该不会太难做到)。