Node.js 执行mongodb请求后如何向前端发送响应?
我正在创建注册页面。其中,我首先检查mongodb数据库中是否已经存在用户电子邮件。如果它存在,那么我想向前端发送错误消息。但是,我没有做到这一点,我认为这可能是因为JavaScript的异步行为Node.js 执行mongodb请求后如何向前端发送响应?,node.js,mongodb,Node.js,Mongodb,我正在创建注册页面。其中,我首先检查mongodb数据库中是否已经存在用户电子邮件。如果它存在,那么我想向前端发送错误消息。但是,我没有做到这一点,我认为这可能是因为JavaScript的异步行为 var myObj , myJSON var SignUpUserEmail, SignUpUserPassword, SignUpUserName, SignUpErr http.createServer(function (req, res) { res.writeHead(200, {'Co
var myObj , myJSON
var SignUpUserEmail, SignUpUserPassword, SignUpUserName, SignUpErr
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
var q = url.parse(req.url, true).query
SignUpUserEmail = q.SignUpUserEmail
SignUpUserPassword = q.SignUpUserPassword
SignUpUserName = q.SignUpUserName
MongoClient.connect("mongodb://localhost:27017/ABC",function(err,
database) {
if (err) throw err;
var db=database.db('ABC')
let findOneParam = {"UserEmail":SignUpUserEmail}
db.collection('Profiles').findOne(findOneParam, function(err, result) {
if (err) throw err;
if(!result) {
db.collection('Profiles', function(err, collection){
if (err) throw err;
collection.insertOne({"UserId":"ProfileA0001",
"UserEmail":SignUpUserEmail,
"UserPassword":SignUpUserPassword,
"UserName":SignUpUserName,
"IsEmailAuthenticated":"false"
}, function(err, res){
if (err) throw err;
SignUpErr = "document inserted"
console.log("SignUpErr inside:", SignUpErr)
})
})
} else {
SignUpErr = "Email already has been registered."
console.log("SignUpErr inside:", SignUpErr)
}
})
})
console.log("SignUpErr outside:", SignUpErr)
myObj = {"SignUpErr":SignUpErr};
myJSON = JSON.stringify(myObj);
res.end(myJSON);
}).listen(9000);
注:“内部签名者:”给出正确的结果。但是,“signuperoutside:”显示为未定义。我通常使用express作为web框架,它附带res.send()方法,您可以在其中发送响应。我通常构建一个JSON响应,并将其作为res.send(JSON.stringify(data))发送;还有res.JSON(数据)。 如果希望使用HTTP模块,则可以使用res.end()方法。 提供了详细信息。希望这有帮助 注:“内部签名者:”给出正确的结果。然而,“外部签名者:”显示为未定义 这是因为NodeJ的异步特性
signuper
将是未定义的
,直到它在db.collection('Profiles',function(){})调用中初始化为止
所以,要解决这个问题,您需要在db.collection('Profiles',function(){})
中发送响应。那就是,在初始化之后
对代码进行这些更改
'use strict';
const http = require('http');
http.createServer(function (req, res) {
res.statusCode = 200; // Setting the status code
res.setHeader('Content-Type', 'text/plain'); // Setting the content-type for response
let {SignUpUserEmail, SignUpUserPassword, SignUpUserName} = url.parse(req.url, true).query;
MongoClient.connect("mongodb://localhost:27017/ABC", function (err, database) {
if (err) {
throw err;
}
let db = database.db('ABC');
db.collection('Profiles').findOne({
UserEmail: SignUpUserEmail
}, function (err, result) {
if (err) {
throw err
}
if (result) {
let msg = "Email already has been registered.";
console.log("SignUpErr inside:", msg);
return res.end(JSON.stringify({
SignUpErr: "document inserted"
}));
}
db.collection('Profiles', function (err, collection) {
if (err) throw err;
collection.insertOne({
"UserId": "ProfileA0001",
"UserEmail": SignUpUserEmail,
"UserPassword": SignUpUserPassword,
"UserName": SignUpUserName,
"IsEmailAuthenticated": "false"
}, function (err, dbresult) {
if (err) {
throw err;
}
let msg = "document inserted";
console.log("SignUpErr inside:", msg);
return res.end(JSON.stringify({
SignUpErr: "document inserted"
}));
})
});
});
});
}).listen(9000);
我按原样使用了你的代码。然而,它不起作用。它给出的错误是“res.status不是一个函数”。@kiranpatil更新了答案。你能检查一下吗?现在我犯了一个错误,因为“res.end不是一个函数”。@kiranpatil啊!注意到我无意中用db.collection('Profiles',function(){})的res
覆盖了responseres
。更新了我的答案。现在请查收。