在Facebook Graph API查询中使用= location时,"未捕获(承诺)未定义"错误

Mic*_*fer 32 javascript facebook-graph-api facebook-javascript-sdk

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

我目前的目标是仅检索附有位置的帖子.

检索包含和不包含位置的帖子已经有效,我无法仅检索包含位置的帖子.

检索这两种类型的查询如下所示: '/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

&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();
}
Run Code Online (Sandbox Code Playgroud)

日志显示以下内容:

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
Run Code Online (Sandbox Code Playgroud)

有没有人有这个可能的解决方案?

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

lus*_*chn 64

该错误告诉您存在错误但您没有抓住它.这就是你如何抓住它:

getAllPosts().then(response => {
    console.log(response);
}).catch(e => {
    console.log(e);
});
Run Code Online (Sandbox Code Playgroud)

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

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

或者使用async/await:

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


小智 7

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

你没有显示使用promise的代码,但我认为它是这样的:

var promise = doSth();
promise.then(function() { doSthHere(); });
Run Code Online (Sandbox Code Playgroud)

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

promise.then(function() { doSthHere(); }, function() {});
Run Code Online (Sandbox Code Playgroud)

这样可以防止出现错误.

但是,我会考虑reject仅在出现实际错误的情况下调用,并且...具有空的异常处理程序并不是最好的编程习惯.