我看到了几个关于这个主题的帖子,但没有结果。一方面,我有一个收集信息(姓名、名字等)然后将其保存在数据库(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);
});
};
Run Code Online (Sandbox Code Playgroud)
和后端路由:
router.post("/signup", function(req, res, next) {
var salt = uid2(32);
console.log("Signup is running...");
const newUser = …Run Code Online (Sandbox Code Playgroud)