【发布时间】:2020-06-23 16:05:21
【问题描述】:
我看到了几篇关于这个主题的帖子,但没有结果。一方面,我有一个收集信息(姓名、名字等)然后将其保存在数据库(mongodb)中的表单。当我使用邮递员通过路由/注册发送我的信息时,一切正常,我可以在 mongodb 中看到我的新用户。但是当我在 Expo 上启动应用程序时,他向我抛出“网络请求失败”。
前端获取:
submitForm = () => {
var signupData = JSON.stringify({
first_name: this.state.firstName,
last_name: this.state.lastName,
email: this.state.email,
password: this.state.password
});
fetch(`https://localhost:3000/signup`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: signupData
})
.then(response => {
console.log(response);
return response.json();
})
.then(data => {
if (data.result) {
this.props.handleUserValid(
this.state.firstName,
this.state.lastName,
this.state.email,
data.user.token
);
this.props.navigation.navigate("Account");
}
})
.catch(error => {
console.error(error);
});
};
和后端路由:
router.post("/signup", function(req, res, next) {
var salt = uid2(32);
console.log("Signup is running...");
const newUser = new userModel({
first_name: req.body.first_name,
last_name: req.body.last_name,
email: req.body.email,
password: SHA256(req.body.password + salt).toString(encBase64),
token: uid2(32),
salt: salt
});
newUser.save(function(error, user) {
console.log("LOG: user", user);
res.json({ result: true, user });
});
});
module.exports = router;
这是错误的截图
再次使用 Postman 时,提取工作正常,打印了我的控制台日志并将用户添加到我的数据库中。 感谢您的帮助。
-- 编辑--
我通过 Expo 在网络浏览器中启动了该应用程序,一切正常。我的登录/注册页面和我的帐户页面。但是在我的手机上它不起作用(IOS),这是我手机的网络问题(可能是证书问题,IP错误?)
如果你有一个我感兴趣的想法,我已经坚持了 2 天
【问题讨论】:
标签: api react-native express request fetch