Javascript 在 Facebook Graph API 查询中使用 with=location 时出现“Uncaught (in promise) undefined”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37624322/
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 20:31:56  来源:igfitidea点击:

"Uncaught (in promise) undefined" error when using with=location in Facebook Graph API query

javascriptfacebook-graph-apifacebook-javascript-sdk

提问by Michael Andorfer

I am currently developing a web application with the Facebook Graph API.

我目前正在使用 Facebook Graph API 开发 Web 应用程序。

My current goal is to retrieve only posts which have a location attached.

我目前的目标是只检索附有位置的帖子。

While retrieving posts with and without location is already working, I am not able to retrieve only posts with location.

虽然检索带有和不带有位置的帖子已经可以工作,但我无法仅检索带有位置的帖子。

The query which retrieves both types looks like this: '/me/feed?fields=id,name,message,picture,place,with_tags&limit=100&with=location'

检索这两种类型的查询如下所示: '/me/feed?fields=id,name,message,picture,place,with_tags&limit=100&with=location'

The query which should retrieve only posts with location looks like this: /me/feed?fields=id,name,message,picture,place,with_tags&limit=100&with=location

应该只检索带有位置的帖子的查询如下所示: /me/feed?fields=id,name,message,picture,place,with_tags&limit=100&with=location

The problem I have is that with the parameter &with=locationI get an error Uncaught (in promise) undefinedat this part of my code:

&with=location我遇到的问题是,使用参数时,我Uncaught (in promise) undefined在代码的这一部分出现错误:

if (response.paging && response.paging.next) {
    recursiveAPICall(response.paging.next);
  } else {
    resolve(postsArr);
  }
} else {
  // Error message comes from here
  reject();
}

Log shows the following:

日志显示如下:

DEBUG: -------------------------------
DEBUG: Ember             : 2.4.5
DEBUG: Ember Data        : 2.5.3
DEBUG: jQuery            : 2.2.4
DEBUG: Ember Simple Auth : 1.1.0
DEBUG: -------------------------------
Object {error: Object}
  error: Objectcode: 
    code: 1
    1fbtrace_id: "H5cXMe7TJIn"
    message: "An unknown error has occurred."
    type: "OAuthException"
    __proto__: Object
  __proto__: Object
Uncaught (in promise) undefined

Does anyone have a possible solution for this?

有没有人对此有可能的解决方案?

For further information how the code looks like see my previous question.

有关代码的详细信息,请参阅我之前的问题

回答by luschn

The error tells you that there is an error but you don′t catch it. This is how you can catch it:

错误告诉你有一个错误,但你没有发现它。这是您可以捕获它的方法:

getAllPosts().then(response => {
    console.log(response);
}).catch(e => {
    console.log(e);
});

You can also just put a console.log(reponse)at the beginning of your API callback function, there is definitely an error message from the Graph API in it.

您也可以console.log(reponse)在 API 回调函数的开头放置一个,其中肯定有来自 Graph API 的错误消息。

More information: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch

更多信息:https: //developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch

Or with async/await:

或者使用异步/等待:

//some async function
try {
    let response = await getAllPosts();
} catch(e) {
    console.log(e);
}

回答by Wojtek Trelak

The rejectactually takes one parameter: that's the exception that occurred in your code that caused the promise to be rejected. So, when you call reject()the exception value is undefined, hence the "undefined" part in the error that you get.

reject其实需要一个参数:这是发生在你的代码导致该承诺被拒绝例外。因此,当您调用reject()异常值为 is 时undefined,因此会出现错误中的“未定义”部分。

You do not show the code that uses the promise, but I reckon it is something like this:

您没有显示使用承诺的代码,但我认为它是这样的:

var promise = doSth();
promise.then(function() { doSthHere(); });

Try adding an empty failure call, like this:

尝试添加一个空的失败调用,如下所示:

promise.then(function() { doSthHere(); }, function() {});

This will prevent the error to appear.

这将防止出现错误。

However, I would consider calling rejectonly in case of an actual error, and also... having empty exception handlers isn't the best programming practice.

但是,我会考虑reject仅在发生实际错误的情况下调用,而且……拥有空的异常处理程序并不是最佳的编程实践。