我正在尝试将从facebook登录的数据登录到我的后端服务器,但是我在解决这个错误时遇到了困难,我似乎无法弄明白。
很可能有人可以帮助我,提前谢谢。
这是我的按钮功能:
facebookSignin() {
FBLoginManager.loginWithPermissions(['public_profile', 'email'], (error, data) => {
if (error) {
console.log(error);
}
let profile;
try {
profile = JSON.parse(data.profile);
console.log(data.profile);
} catch (error) {
console.log(error);
}
/* signin on server with token */
return fetch(`http://${this.serverHost}/users/signin/provider`, {
method: 'POST',
headers: new Headers({ 'Content-Type': 'application/json' }),
body: JSON.stringify({
provider: 'facebook',
id: profile.id,
email: profile.email,
name: profile.name,
token: data.credentials.token
})
})
.then(data => data.json())
.then(data => {
/* get user data and auth token from server */
console.log(data);
});
});
}
我收到错误:
"无法阅读财产' id'未定义"
答案 0 :(得分:1)
您解析try
块中的数据,但尝试访问其外的person
数据。
如果您JSON.parse
收到错误,则变量persons
仍为undefined
。当您尝试阅读id
道具时,您将收到错误"Cannot read property 'id' of undefined"
。
在fetch
;
try
代码块放在JSON.parse
内