我正在构建一个使用JWT进行身份验证的银行应用程序.提交登录表单后,服务器给出了响应,但由于某种原因,我无法从服务器获取数据,它一直给出不确定的信息,这很奇怪,因为我可以清楚地看到服务器正在给出响应.
下面是我获取数据的方式
onClick={(event)=>{
event.preventDefault()
let user={tel, password}
fetch('http://localhost:3000/login', {
method:'POST',
headers:{'Content-Type':'application/json'},
body:JSON.stringify(user)
})
.then(response=>(response.json()))
.then(data=>localStorage.setItem('user',JSON.stringify(data)))
.then(navigate('/home'))
.then(window.location.reload)
.catch(error=>{console.log('error sending data to server')})
}}
当我没有离开页面时,我可以在空白页面上看到服务器生成的令牌
当前在Express服务器上处理邮件的API
app.post('/login', (req,res,next)=>{
let loginuser=req.body
console.log(loginuser)
let founduser
Account.find({tel:loginuser.tel}).exec().then(response=>{founduser=response.lastname})
if (loginuser){
Account.find({tel:loginuser.tel}, ['userpassword']).exec().then(response=>{
//New users will require bcrypt.compare() to log-in
if(response.length!== 0){
if(loginuser.password===response[0].userpassword){
console.log('found user')
res.setHeader('Access-Control-Allow-Origin', '*');
res.json({token:jwt.sign({lastname:founduser},SECRET)})
next()
}
else{
console.log('hey! check input')
res.json({errormsg:'Phone number and password mismatch'})
}}
else {
console.log('user not found')
res.json({errormsg:'User not found'})
}
})
}
})
来自服务器的响应
{ tel: '0123456789', password: '' }
found user
浏览器控制台控制台
error sending data to server
从上面看,有一些东西触发了Fetch API的错误处理程序,有人能解释一下为什么会这样吗?我做错了什么,我非常感激任何帮助
我试过return fetch('')
次了
添加setTimeout()以显示读取的数据
我正在使用带有node/exts.js的reactjs,Mongoose使用ODM