Javascript -Uncaught 承诺被拒绝,即使它已经被拒绝

2024-04-06

我正在运行这个 Parse.com 云代码作业。它查询我的一个类并获取 url,然后我读取这些 url(它们是 xml 文件),然后从其中获取一些数据并将其保存以进行解析。从代码中可以看出。

这是代码

完整代码在这里gist.github.com/gouegd/aae61aa08b8295d52b08 https://gist.github.com/gouegd/aae61aa08b8295d52b08

当我运行这个云代码作业时。在控制台中。我看到这条消息

Failed with: Uncaught A promise was rejected even though it had already been rejected.

出现这种情况是因为某些网址无效,然后代码就中断了。

基本上我需要一种方法来处理它,当其中一个 URL 不起作用并且不让代码停止时!并继续其他网址。

这个问题发生在第 77-83 行之间,其中正在传递 url 变量,因此我需要它忽略错误的 url,然后继续处理其余的 url。

我在这里先向您的帮助表示感谢。


这是一个奇怪的错误消息。

据我所知...

在第 89 行和第 90 行之间插入:

    }, function() {
        return Parse.Promise.as();//return resolved promise to keep the promise chain going.

给予:

return Parse.Cloud.httpRequest({
    url: url,
    //data: ... //some properties of menuItem?
}).then(function(httpResponse) {
    return readResponse_async(httpResponse.text).then(function(res) {
        if (res.menu.day.at(dayNumber).meal) {

            return saveMeals_async(res.menu.day.at(dayNumber).meal, school, diningHallNumber, menuLocation);
        } else {
            return Parse.Promise.as();//return resolved promise to keep the promise chain going.
        }
    }, function() {
        return Parse.Promise.as();//return resolved promise to keep the promise chain going.
    });
});

或者可能低一行:

return Parse.Cloud.httpRequest({
    url: url,
    //data: ... //some properties of menuItem?
}).then(function(httpResponse) {
    return readResponse_async(httpResponse.text).then(function(res) {
        if (res.menu.day.at(dayNumber).meal) {

            return saveMeals_async(res.menu.day.at(dayNumber).meal, school, diningHallNumber, menuLocation);
        } else {
            return Parse.Promise.as();//return resolved promise to keep the promise chain going.
        }
    });
}, function() {
    return Parse.Promise.as();//return resolved promise to keep the promise chain going.
});

EDIT

由于这两个都未能处理错误,您可以尝试这个,这很混乱,但可以容忍,如果我怀疑 Parse 的承诺不是“抛出安全”:

return Parse.Cloud.httpRequest({
    url: url,
    //data: ... //some properties of menuItem?
}).then(function(httpResponse) {
    try {
        return readResponse_async(httpResponse.text).then(function(res) {
            if (res.menu.day.at(dayNumber).meal) {
                return saveMeals_async(res.menu.day.at(dayNumber).meal, school, diningHallNumber, menuLocation);
            } else {
                throw new Error();
            }
        });
    }
    catch(e) {
        return Parse.Promise.as();//return resolved promise to keep the promise chain going.
    }
}, function() {
    return Parse.Promise.as();//return resolved promise to keep the promise chain going.
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Javascript -Uncaught 承诺被拒绝,即使它已经被拒绝 的相关文章