这是我处理注册的代码
handleSignUp(){
console.log('Clicked')
fetch('http://laptopIp:9999/signUp',{
method:'POST',
headers:{
Accept:'application/json',
'Content-Type':'application/json',
},
body:JSON.stringify(this.state),
})
.then((res)=>{
console.log("here")
console.log(res)
this.setState({error:false})
}).catch((e)=>{
console.log(e)
console.log("in error")
})
这是处理请求的快速服务器
router.post('/signUp',function(reqs,resp){
MongoClient.connect(url,{ useNewUrlParser: true },function(err,database){
if(err){
console.log(err)
}else{
var dataBases = database.db("thaparApp")
dataBases.collection('userLogin').find({'email':reqs.body.email}).toArray(function(err,result){
if(err){
console.log(err)
}else if(result[0]){
resp.status(403).send({"error":"email is taken"})
}else{
if(validateEmail(reqs.body.email)){
dataBases.collection('userLogin').insertOne(reqs.body)
resp.send()
}else{
resp.status(403).send({"error":"Email is not correct"})
}
}
})
}
})
}) }
我正在做的是从 react-native 发送相同的用户名,express 向我发送错误403,但React Native 没有在 .catch() 中处理该错误,而是将其保留在 .then() 中
正如我们在图像中看到的那样,状态代码是 403,这是错误,并且.catch()
没有打印的 console.log("in error") 。
调用从服务器返回,虽然它的状态不是你所期望的,但它不是一个错误。
尝试使用 fetchok
来检查响应是否有效,例如:
.then(function(response) {
if (!response.ok) {
//throw error
} else {
//valid response
}
})
您还可以访问响应的状态,例如:
response.status;
或者,statusText 如:
response.statusText;
结帐以下:
https://developer.mozilla.org/en-US/docs/Web/API/Response/statusText
https://developer.mozilla.org/en-US/docs/Web/API/Response/status
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句