Node.js 在函数中传递时未定义响应对象
我正在使用Facebook Graph NodeJS API获取Node.js 在函数中传递时未定义响应对象,node.js,express,facebook-node-sdk,Node.js,Express,Facebook Node Sdk,我正在使用Facebook Graph NodeJS API获取用户帖子。响应有分页,因此我需要在响应上循环以获取所有帖子。我使用以下路径获取facebook帖子,并使用get_user_status功能循环分页: var posts = ""; function get_user_statuses(response_posts, res) { var link_regex = /https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\
用户帖子
。响应有分页,因此我需要在响应上循环以获取所有帖子。我使用以下路径获取facebook帖子,并使用get_user_status
功能循环分页:
var posts = "";
function get_user_statuses(response_posts, res) {
var link_regex = /https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,}/g;
var isNextPageAvailable = true;
if ("paging" in response_posts) {
var nextPage = response_posts.paging.next;
isNextPageAvailable = true;
} else {
isNextPageAvailable = false;
}
for (var i = 0; i < response_posts.data.length; i++) {
var post = response_posts.data[i].message + " ";
if ("message" in response_posts.data[i]) {
posts += post.replace(link_regex, "");
}
}
if (nextPage !== undefined) {
request(nextPage, function (error, response, body) {
if (!error && response.statusCode === 200) {
get_user_statuses(JSON.parse(body));
} else {
console.log(error);
}
});
}
if (!isNextPageAvailable){
//Sending posts to facebook Modal
console.log(posts);
res.send(JSON.stringify({posts: posts})); //res is not defined here
}
}
router.post('/fbData', function (req, response, next) {
FB.setAccessToken(req.body.access_token);
FB.api('/me?fields=posts&posts.limit=1000', function (res) {
if (!res || res.error) {
if (!res) {
response.send(JSON.stringify({error: 'An error occurred. Please try again.'}))
}
response.send(JSON.stringify({error: response.error}));
return;
}
get_user_statuses(res.posts, response); //Passing response object here
});
});
var posts=”“;
功能获取用户状态(回复帖子、回复){
var link_regex=/https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}www\.[^\s]+\.[^\s]{2,}/g;
var isNextPageAvailable=真;
如果(“寻呼”在回应中){
var nextPage=response_posts.paging.next;
isNextPageAvailable=true;
}否则{
isNextPageAvailable=false;
}
对于(var i=0;i
问题在于,在get\u user\u status
函数中未定义从expressroute
传递的response
对象。现在我有两个问题:
我解决了我的问题。我需要创建一个带有回调的函数。如果其他人陷入这种问题,这篇文章帮助我解决了它:
[
res
没有定义,因为您忘记在内部调用中传递它。将获取用户状态(JSON.parse(body));
更改为获取用户状态(JSON.parse(body),res);
并且它应该可以工作嘿,感谢您的响应。我发现使用我在回答中提到的链接可以实现我的目标。