在 Facebook Graph API 查询中使用 with=location 时出现“未捕获(承诺中)未定义”错误

问题描述 投票:0回答:4

我目前正在使用 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();
}

日志显示以下内容:

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

有人有可能解决这个问题吗?

有关代码的更多信息,请参阅我的上一个问题

javascript facebook-graph-api facebook-javascript-sdk
4个回答
149
投票

该错误告诉您存在错误,但您没有发现它。这是你可以抓住它的方法:

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

你也可以在API回调函数的开头加上一个

console.log(reponse)
,里面肯定有来自Graph API的错误消息。

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

或者使用异步/等待:

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

22
投票

reject
实际上有一个参数:这是代码中发生的导致 Promise 被拒绝的异常。因此,当您调用
reject()
时,异常值为
undefined
,因此您得到的错误中的“未定义”部分。

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

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

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

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

这将防止出现错误。

但是,我会考虑仅在实际错误的情况下调用

reject
,而且......拥有空的异常处理程序并不是最佳的编程实践。


0
投票
  1. 打开 Chrome 开发工具
  2. 开源选项卡
  3. 确保调试器面板已打开(最右侧的侧边栏),否则使用此按钮展开它:
  4. 在“断点”部分中,选中两个框以在出现异常时暂停
  5. 重新加载页面并执行任何其他步骤来触发错误
  6. 调试器暂停后,检查代码。有时,如果你幸运的话,它会具有足够的描述性,或者在
    reject()
    启动之前会出现一个控制台日志。

0
投票

正确的端点 URL: **“https://**us-central1-alskalk-206421.cloudfunctions.net/gcip-connect”

不正确的端点 URL:

httpshttps://us-99central1-alskalk-206421.cloudfunctions.net/gcip-connect” “htt://us-central1-alskalk-206421.cloudfunctions.net/gcip-connect” 使用不正确的端点 URL 时会发生此错误。确保 URL 格式正确,以“https://”开头,并且没有任何重复或缺失部分。

enter code here

© www.soinside.com 2019 - 2024. All rights reserved.