[英]Node continues execution even though a response has been sent
I have something along the lines of the below code:我有一些类似于以下代码的内容:
const checkForSpaces = text => !/\s/g.test(text)
const verifyLink = async (res) => {
return res.status(400).end()
}
router.post("/", async (req, res) => {
const { email, password, verifyToken } = req.body
if (verifyingAdmin) {
await verifyLink(res, verifyToken)
}
// Code below this line should not be reached
try {
if (!checkForSpaces(email)) {
return res.status(400).json({ msg: "Email cannot have spaces" })
}
. . . .
There is a use case where email
and password
are null
and in that use case, in verifyLink
, I send a response back to the frontend, however I run into an error, Cannot read property 'trim' of null
for the obvious reason that those aforementioned fields are null
.有一个用例
email
和password
是null
并且在那个用例中,在verifyLink
中,我将响应发送回前端,但是我遇到了一个错误, Cannot read property 'trim' of null
的明显原因上述字段是null
。 What am I doing wrong here?我在这里做错了什么?
The execution goes on because you don't return from the function.执行继续,因为您没有从 function 返回。 Just add a
return
after await verifyLink(res, verifyToken)
.只需在
await verifyLink(res, verifyToken)
之后添加一个return
即可。 The return
in verifyLink
will only end the execution of the verifyLink
function, not of the parent function. verifyLink
中的return
只会结束verifyLink
function 的执行,而不是父 function 的执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.