简体   繁体   English

即使已发送响应,节点也会继续执行

[英]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 .有一个用例emailpasswordnull并且在那个用例中,在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.

相关问题 jQuery验证程序继续,即使未满足所有规则 - jquery validator continues even though all rules have not been met While 循环不会停止,即使条件已经满足。 它又持续了 9 次。 没有逻辑意义 - While loop won't stop, even though the condition has been met. It continues for another 9 times. Makes no logical sense 访问已被 CORS 策略阻止,即使预检响应成功“Access-Control-Allow-Origin”通配符存在 - Access has been blocked by CORS policy even though preflight Response is successful 'Access-Control-Allow-Origin' wildcard exists 即使已发送变量包含数据,jQuery帖子仍为空 - Jquery Post Empty Even though Sent Variable has data 即使调用clearInterval,间隔也会继续触发 - interval keeps firing even though clearInterval has been called 即使未分派动作,Redux状态也会在加载时发生变化 - Redux state changes on load even though no action has been dispatched 'No Firebase App '[DEFAULT]' has been created' 即使调用了 initializeApp - 'No Firebase App '[DEFAULT]' has been created' even though initializeApp is called 即使已给定value =“”,也可以显式清除表单字段 - Explicitly clear form field even though value=“ ” has been given Function 在返回后继续执行 - Function continues execution even after return 在Koa发送响应后运行代码 - Running code AFTER the response has been sent by Koa
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM