javascript - console.log 什么都不返回
问题描述
我正在尝试设置一个登录屏幕,以便让用户通过 expo-facebook 通过 facebook 登录。我想在 console.log 中检索用户的姓名和电子邮件,以进行测试。但是,我在终端中什么都没有。你能告诉我为什么吗?但是,我的帐户可以正常连接。谢谢你的吹捧助手。
logInViaFacebook = async () => {
try {
await Facebook.initializeAsync("34904");
const {
type,
token,
expires,
permissions,
declinedPermissions,
} = await Facebook.logInWithReadPermissionsAsync({
permissions: ["public_profile", "email"],
});
if (type === "success") {
// Get the user's name using Facebook's Graph API
const response = await fetch(
`https://graph.facebook.com/me?fields=id,name,email,birthday&access_token=${token}`
);
this.setState({
signedIn: true,
name: response.name,
email: response.email,
});
console.log(this.state.name, this.state.email,)
this.props.navigation.navigate("TabBar");
// saving the token in AsyncStorage
//await AsyncStorage.setItem('email', email); // <- save the email in AsyncStorage for later use
} else {
// type === 'cancel'
}
} catch ({ message }) {
alert(`Facebook Login Error: ${message}`);
}
}
解决方案
setState
是异步的,因此您无法立即看到更新。要查看更新,您需要将回调作为 setState 的第二个参数传递。
this.setState({
signedIn: true,
name: response.name,
email: response.email,
}, () => {
console.log(this.state.name, this.state.email);
});
推荐阅读
- r - 色彩空间 R 包是 Tidyverse 的一部分吗?(相互矛盾的信息)
- javascript - 如何处理在 JS(组合键)上按 C++/ 键并调用某些功能?
- mysql - SQL 查询,如何在某个字段上执行 sum(),但如果其中一个字段 sum() 中的值为 null,则回退到“NA”
- angular - angular2 *ngFor 和 *ngIf 不更新
- c# - 如何通过触摸而不是触摸球体对撞机来打开和关闭灯?
- salesforce - 从流调用时无法使用 Apex 创建报价 PDF
- javascript - 图表 JS 插件根据值更改线条颜色
- modelica - 准固定交流电到“正常”交流电,WindPowerPlants
- dart - 根据另一个变量的类型转换(嵌套列表)对象
- asp.net - 忽略开发环境的授权