Node.js:Facebook返回的是未定义而不是个人资料
Node.js: Facebook returns me undefined instead of profile?
我想在我的应用程序上进行Google和Facebook身份验证。我在前端使用AngularJS
,在服务器上使用node.js
。
这是我在node.js
服务器上的Facebook身份验证功能:
var User = require('./user.js'),
request = require('request'),
env = process.env.NODE_ENV = process.env.NODE_ENV || 'development',
config = require('./config.js')[env],
createSendToken = require('./jwt.js'),
qs = require('querystring');
module.exports = function (req, res) {
var accessTokenUrl = 'https://graph.facebook.com/oauth/access_token';
var graphApiUrl = 'https://graph.facebook.com/me';
var params = {
client_id: req.body.clientId,
redirect_uri: req.body.redirectUri,
client_secret: config.facebookAuth.clientSecret,
code: req.body.code
};
console.log(req.body.code);
request.get({
url: accessTokenUrl,
qs: params
}, function (err, response, accessToken) {
accessToken = qs.parse(accessToken);
request.get({
url: graphApiUrl,
qs: accessToken,
json: true
}, function (err, response, profile) {
User.findOne({
facebookId: profile.id // <-- fails here
}, function (err, existingUser) {
if (existingUser)
return createSendToken(existingUser, res);
var newUser = new User();
newUser.facebookId = profile.id;
newUser.displayName = profile.name;
newUser.email = profile.email;
newUser.save(function (err) {
createSendToken(newUser, res);
})
})
});
});
}
然后分配给此post
:
app.post('/auth/facebook', facebookAuth);
前端工作正常(我正在使用satellizer
)。
我在Google+上做了几乎同样的事情,它工作得很好,但在Facebook上却不是。
我的Facebook应用程序配置正确(我已将http://localhost:3030/
添加到网站URL和移动网站URL)。
现在,当我运行我的应用程序并按Facebook按钮时,我在服务器上收到以下错误:
facebookId: profile.id
^
TypeError: Cannot read property 'id' of undefined
所以这意味着profile
是undefined
:
}, function (err, response, profile) {
为什么Facebook给我发了一个未定义的个人资料?
我该如何解决这个问题?
检查 中 err
的值
json: true
}, function (err, response, profile) {
User.findOne({
facebookId: profile.id // <-- fails here
回答您的问题
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- $window.ga在AngularJS事件中未定义
- 未捕获的TypeError无法读取未定义的属性socialsharing
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 无法获取属性'selectedIndex'的未定义引用或null引用
- 如何消除代码中的未定义和其他问题
- 未捕获的ReferenceError:$未定义
- this.router在AngularJS 2中未定义
- 未捕获的类型错误:无法读取属性'删除'的未定义
- 节点fs.stat名称未定义
- 尝试在PHP中回显输入文本时出现未定义的索引错误
- Jquery未定义函数正在停止其他操作
- 使用Facebook登录时未填写个人资料
- 护照个人资料未通过新属性
- 未捕获的引用错误:未定义个人
- Node.js:Facebook返回的是未定义而不是个人资料
- 如何用JS显示自定义图标而不是个人资料图像的Twitter feed