我在获取函数后使用.then(jsonResponse =>
,我对其进行了字符串化处理并将其登录到控制台中,以查看api是否将确认键返回为“成功”,即使它返回的成功语句如下:< / p>
if (jsonResponse.confirmation === "success") {
this.props.navigation.navigate("main");
}
不起作用,它无法导航到“主”屏幕,有人知道如何解决此问题吗?我一直在论坛中寻找答案,但找不到,这是代码的组织方式:
register() {
fetch(config.baseUrl + "signup", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(this.state.credenciais)
}) //all of this works, since it connects to the api and registers the user with its credentials
.then(response => response.json())
.then(jsonResponse => {
console.log(JSON.stringify(jsonResponse)) //this returns as success
console.log(response) // returns as undefined
if (jsonResponse.confirmation === "success") {
this.props.navigation.navigate("main");
} else {
throw new Error({
message: "Algo ocorreu de errado, por favor tente novamente"
});
}
})
.catch(err => {
console.log(err.message);
});
}
我评论了我无法弄清楚的部分,并在api中将其作为路线:
router.post("/signup", function(req, res) {
console.log(req.body);
turbo
.createUser(req.body)
.then(data => {
res.json({
confirmation: "success",
data: data
});
})
.catch(err => {
res.json({
confirmation: "fail",
message: err.message
});
});
});
如您所见,我得到了确认:如果一切顺利,上面的代码可以正常工作并注册用户,但无法导航到“主”屏幕,有人知道我的代码有什么问题吗?
编辑:如下所示,这是console.log(JSON.stringify(jsonResponse))
{“确认”:“成功”,“数据”:{“名字”:“”,“姓氏”:“”,“电子邮件”:“ testestes@aol.com”,“用户名”:“”, “ bio”:“”,“ image”:“”,“ timestamp”:“ 2018-11-03T16:16:00.855Z”,“ stripe”:{},“ schema”:“ user”,“ id”: “ 5bddc9c050edeb001431cb2e”}}
此外,在TouchableOpacity中调用register():
<TouchableOpacity
onPress={() => {
this.register();
}}
>