Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在节点中写入条件签入?_Javascript_Json_Node.js - Fatal编程技术网

Javascript 如何在节点中写入条件签入?

Javascript 如何在节点中写入条件签入?,javascript,json,node.js,Javascript,Json,Node.js,我得到以下例外情况: throw er; // Unhandled 'error' event ^ Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at validateHeader (_http_outgoing.js:503:11) at ServerResponse.setHeader (_http_outgoing.js:510:3)

我得到以下例外情况:

 throw er; // Unhandled 'error' event
      ^

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at validateHeader (_http_outgoing.js:503:11)
    at ServerResponse.setHeader (_http_outgoing.js:510:3)
    at ServerResponse.header (/Users/athulmuralidharan/my_documents/MS/MSD/projects/MLL-backEnd/node_modules/express/lib/response.js:730:10)
    at ServerResponse.send (/Users/athulmuralidharan/my_documents/MS/MSD/projects/MLL-backEnd/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/Users/athulmuralidharan/my_documents/MS/MSD/projects/MLL-backEnd/node_modules/express/lib/response.js:256:15)
    at /Users/athulmuralidharan/my_documents/MS/MSD/projects/MLL-backEnd/controllers/users.js:56:13
    at model.Query.<anonymous> (/Users/athulmuralidharan/my_documents/MS/MSD/projects/MLL-backEnd/node_modules/mongoose/lib/model.js:3928:16)
    at /Users/athulmuralidharan/my_documents/MS/MSD/projects/MLL-backEnd/node_modules/kareem/index.js:297:21
    at /Users/athulmuralidharan/my_documents/MS/MSD/projects/MLL-backEnd/node_modules/kareem/index.js:135:16
    at process._tickCallback (internal/process/next_tick.js:150:11)

如果
err
是真的,您的程序将
res.send
,它将头发送到客户端

if (err)
    res.send(err);
但是,在此之后您不会停止程序,因此下一个if语句也会运行,并且有可能
obj
等于null,因此您已经使用了
res.send(err)
,但您尝试
res.status(404)

要解决此问题,只需在使用
res.send
、使用
else
语句或返回语句后停止程序:

if (err) {
    res.send(err);
} else if (obj == null) {
    console.log("null returned");
    res.status(404).send("Oh uh, something went wrong");
}
或者

if (err)
    return res.send(err);

if (obj == null) {
    console.log("null returned");
    res.status(404).send("Oh uh, something went wrong");
}

你真的只是错过了一个事实

User.findOne({email: req.body.username,password: req.body.password}, function(err,obj) {
    if (err)
        return res.send(err);

    if (obj == null)
    {
        console.log("null returned");
        return res.status(404).send("Oh uh, something went wrong");

    }
    console.log(obj);
    res.json(obj);
});

这是因为当出现
err
时,正如错误所说,您发送了多个标题。

可能重复nope。您指向的错误表明它已在使用中。只有在添加return@AthulMuralidharan是的,很明显,b/c在您使用
.send
之后,您只需停止程序到达
.status
if (err)
    return res.send(err);

if (obj == null) {
    console.log("null returned");
    res.status(404).send("Oh uh, something went wrong");
}
User.findOne({email: req.body.username,password: req.body.password}, function(err,obj) {
    if (err)
        return res.send(err);

    if (obj == null)
    {
        console.log("null returned");
        return res.status(404).send("Oh uh, something went wrong");

    }
    console.log(obj);
    res.json(obj);
});