adi*_*ius 11
正如 @kasho 所解释的,这只是短路函数所必需的。但是,我不建议返回send()呼叫本身,而是在呼叫之后返回。否则它会给出错误的表达式,即调用的返回值send()很重要,但事实并非如此,因为它只是undefined.
if (!isAuthenticated) {
res.sendStatus(401)
return
}
res
.status(200)
.send(user)
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您有条件并且想提前退出,您可以使用 return,因为多次调用 res.send() 会引发错误。例如:
//...Fetch a post from db
if(!post){
// Will return response and not run the rest of the code after next line
return res.status(404).send({message: "Could not find post."})
}
//...Do some work (ie. update post)
// Return response
res.status(200).send({message: "Updated post successfuly"})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2960 次 |
| 最近记录: |