gpt4 book ai didi

node.js - 如果我使用 send 或 json, Node 应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 03:20:03 24 4
gpt4 key购买 nike

我正在创建一个 IOS 应用程序,在后台我们使用了 Node.js 和 Mongodb。我已经创建了用于创建用户的 Node 应用程序,并通过 json 发送错误和成功响应,但如果使用 res.send 我的 Node 应用程序将崩溃。我试图发现问题,但尚未得到积极的回应。下面是我的所有代码。

Controller :

const nodemailer    = require('nodemailer');
const passport = require('passport');
const User = require('../../models/User');





exports.ManuallySave = function(req,res)
{
if(req.body.my_token !== '')
{
User.findOne({ email: req.body.email }, (err, existingUser) => {
if (existingUser)
{
//res.setHeader('Content-Type', 'text/plain');
res.json({"status":'error',"msg":'Email address already exists.'});
}
});

User.findOne({ username: req.body.username }, (err, existingUserName) => {
if (existingUserName)
{
res.send({"status":'error',"msg":'Username already exists.'});
}
});

/* Save Action perform */
}
else
{
res.send({"status":'error',"msg":'Token is not available.'});
}

}

控制台错误。

/var/www/node/MyApplication/node_modules/mongodb/lib/utils.js:98
process.nextTick(function() { throw err; });
^

Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
at ServerResponse.header (/var/www/node/MyApplication/node_modules/express/lib/response.js:718:10)
at ServerResponse.send (/var/www/node/MyApplication/node_modules/express/lib/response.js:163:12)
at ServerResponse.json (/var/www/node/MyApplication/node_modules/express/lib/response.js:249:15)
at ServerResponse.send (/var/www/node/MyApplication/node_modules/express/lib/response.js:151:21)
at /var/www/node/MyApplication/controllers/apis/userAppController.js:55:13
at model.<anonymous> (/var/www/node/MyApplication/node_modules/mongoose/lib/document.js:1875:20)
at next_ (/var/www/node/MyApplication/node_modules/hooks-fixed/hooks.js:89:34)
at fnWrapper (/var/www/node/MyApplication/node_modules/hooks-fixed/hooks.js:186:18)
at /var/www/node/MyApplication/node_modules/mongoose/lib/model.js:226:5
at /var/www/node/MyApplication/node_modules/mongoose/lib/model.js:135:7
at /var/www/node/MyApplication/node_modules/mongodb/lib/collection.js:504:5
at /var/www/node/MyApplication/node_modules/mongodb/lib/collection.js:666:5
at handleCallback (/var/www/node/MyApplication/node_modules/mongodb/lib/utils.js:96:12)
at /var/www/node/MyApplication/node_modules/mongodb/lib/bulk/unordered.js:473:9
at handleCallback (/var/www/node/MyApplication/node_modules/mongodb/lib/utils.js:96:12)
at resultHandler (/var/www/node/MyApplication/node_modules/mongodb/lib/bulk/unordered.js:420:5)
at /var/www/node/MyApplication/node_modules/mongodb-core/lib/wireprotocol/2_4_support.js:544:17
at nextTickCallbackWith0Args (node.js:420:9)
at process._tickCallback (node.js:349:13)

我已经使用了 res.send 和 res.json,但在这两种情况下我的应用程序都会崩溃。

最佳答案

当您在已经调用 res.send 的情况下尝试调用 res 上的方法时,会引发该错误。不能保证 res.send 在您的代码中只会被调用一次,而这是必须的。区 block

User.findOne({ email: req.body.email }, (err, existingUser) => {
if (existingUser)
{
//res.setHeader('Content-Type', 'text/plain');
res.json({"status":'error',"msg":'Email address already exists.'});
}
});

User.findOne({ username: req.body.username }, (err, existingUserName) => {
if (existingUserName)
{
res.send({"status":'error',"msg":'Username already exists.'});
}
});

如果您已经有一个用户同时拥有该电子邮件地址和该用户名,则将调用 res.send 两次。您必须在第一个回调中执行另一个调用。

User.findOne({ email: req.body.email }, (err, existingUser) => {
if (existingUser)
{
//res.setHeader('Content-Type', 'text/plain');
res.json({"status":'error',"msg":'Email address already exists.'});
} else {
checkUsername();
}
});
function checkUsername() {
User.findOne({ username: req.body.username }, (err, existingUserName) => {
if (existingUserName)
{
res.send({"status":'error',"msg":'Username already exists.'});
} else {
// save action
}
});
}

我建议您查看promise chains处理不可避免的即将到来的回调嵌套?

关于node.js - 如果我使用 send 或 json, Node 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38889407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com