TypeError: Failed to fetch REACT JS
TypeError: Failed to fetch REACT JS
所以我正在编写一个应用程序,当我输入正确的电子邮件地址(如 user@gmail.com 时,我试图注册一个用户,我给了我 TypeError: Failed to fetch 但是当我输入无效的电子邮件时,如 "用户”它 运行 正确。
下面是SignUp.js代码
const SignUp =()=>{
const history=useHistory()
const [name,setName]=useState("")
const [password,setPassword]=useState("")
const [email,setEmail]=useState("")
const PostData =()=>{
// if(!/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(email)){
// M.toast({html: 'Invalid email', classes: '#0d47a1 blue darken-4'})
// return
// }
fetch("/signup",{
method:"post",
headers:{
"Content-Type":"application/json",
'Accept': 'application/json'
},
body:JSON.stringify({
name,
email,
password
})
}).then(res=>res.json())
.then(data=>{
console.log(data)
if(data.error){
M.toast({html: data.error, classes: '#0d47a1 blue darken-4'});
}
else{
// M.toast({html: data.message, classes: '#0d47a1 blue darken-4'});
history.push('/signin')
}
}).catch(err=>{
console.log(err)
})
}
下面是我的signup.js(服务器端代码)
router.post("/signup", (req, res) => {
const { name, email, password } = req.body
if (!email || !password || !name) {
return res.status(422).json({ error: "Please add all the fields" })
}
User.findOne({ email: email })
.then((savedUser) => {
if (savedUser) {
return res.status(422).json({ error: "User Already Exists" })
}
bcrypt.hash(password, 12)//default is 10
.then(hashedpassword => {
const user = new User({
email,
password:hashedpassword,
name
})
user.save()
.then(user => {
console.log(user)
res.json({ message: 'User Joined BigBrains' })
})
.catch(err => {
console.log(err)
})
})
}).catch(err => {
console.log(err)
})
})
我已经通过 Postman 检查了我的后端,运行宁是正确的。但是当我 运行 它通过客户端(前端)时,它给我错误的正确数据输入。
请帮忙。我是开发新手,但我知道我的逻辑是正确的我不知道为什么会出现此错误
ERRORSNIP
谢谢。我已经解决了我的问题 我正在创建无效的网络地址 我已经卸载了节点模块并重新安装了 npm 以及所有 w
所以我正在编写一个应用程序,当我输入正确的电子邮件地址(如 user@gmail.com 时,我试图注册一个用户,我给了我 TypeError: Failed to fetch 但是当我输入无效的电子邮件时,如 "用户”它 运行 正确。 下面是SignUp.js代码
const SignUp =()=>{
const history=useHistory()
const [name,setName]=useState("")
const [password,setPassword]=useState("")
const [email,setEmail]=useState("")
const PostData =()=>{
// if(!/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(email)){
// M.toast({html: 'Invalid email', classes: '#0d47a1 blue darken-4'})
// return
// }
fetch("/signup",{
method:"post",
headers:{
"Content-Type":"application/json",
'Accept': 'application/json'
},
body:JSON.stringify({
name,
email,
password
})
}).then(res=>res.json())
.then(data=>{
console.log(data)
if(data.error){
M.toast({html: data.error, classes: '#0d47a1 blue darken-4'});
}
else{
// M.toast({html: data.message, classes: '#0d47a1 blue darken-4'});
history.push('/signin')
}
}).catch(err=>{
console.log(err)
})
}
下面是我的signup.js(服务器端代码)
router.post("/signup", (req, res) => {
const { name, email, password } = req.body
if (!email || !password || !name) {
return res.status(422).json({ error: "Please add all the fields" })
}
User.findOne({ email: email })
.then((savedUser) => {
if (savedUser) {
return res.status(422).json({ error: "User Already Exists" })
}
bcrypt.hash(password, 12)//default is 10
.then(hashedpassword => {
const user = new User({
email,
password:hashedpassword,
name
})
user.save()
.then(user => {
console.log(user)
res.json({ message: 'User Joined BigBrains' })
})
.catch(err => {
console.log(err)
})
})
}).catch(err => {
console.log(err)
})
})
我已经通过 Postman 检查了我的后端,运行宁是正确的。但是当我 运行 它通过客户端(前端)时,它给我错误的正确数据输入。
请帮忙。我是开发新手,但我知道我的逻辑是正确的我不知道为什么会出现此错误 ERRORSNIP
谢谢。我已经解决了我的问题 我正在创建无效的网络地址 我已经卸载了节点模块并重新安装了 npm 以及所有 w