gpt4 book ai didi

node.js - Nodejs表达如何将响应发送回浏览器

转载 作者:太空宇宙 更新时间:2023-11-04 03:20:53 25 4
gpt4 key购买 nike

我正在尝试使用 AngularJS 和 Express 在 NodeJS 中制作登录应用程序。我从 angularjs 向服务器发送 $http post 请求,首先它应该检查数据是否已经存在于 MongoDB 中并将响应发送回客户端。

问题是,我无法使用 header 信息配置响应。每次我得到

Error: Can't set headers after they are sent.

MongoError: E11000 duplicate key error collection

Here is my app.js code.

app.post('/signin',function (req,res) {
console.log('i recievied a get request in app.js = /signin');
console.log(req.body);
db.users.findOne({$or: [{username: req.body.username}, {email: req.body.email}]},function (err, data){
if ( err ) throw err;
if (err) { res.send(err); return; }
//res.json({ data: "Invalid Password" });
if ( err ) {console.log('user found already in db'); console.log(err)};
//res.json(data);
//console.log('user found already in db')
//res.redirect('/foo/bar');
res.end('user found already in db');
//res.send({ data: 'user found already in db' })
//res.setHeader('Content-Type', 'application/json');
//res.json({ a: 1 });
//res.writeHead(200, {'Content-Type': 'text/plain'});
//res.send("jai shree ram");
//res.end();
//return;
});

db['users'].insert({
username : req.body.username,
firstname : req.body.firstname,
lastname : req.body.lastname,
email : req.body.email,
password : req.body.password,
createdAt : new Date(),
role : "user",
approvedBy : req.body.approver.username,
status : "locked"
},function (err, data){
if ( err ) throw err;
//if ( err ) {console.log('user created in db'); console.log(err)};
//res.json(data);
//if (err) { res.send(err); return; }
//res.json({ data: "Password" });
res.end('user created in db');
//res.send({ data: 'user created in db' })
//res.setHeader('Content-Type', 'application/json');
//res.json({ a: 1 });
//res.end();
//return;
})

});

我从浏览器发出以下请求

$http({
url: '/signin',
method: "POST",
data: $scope.SignInForm
//headers: {'Content-Type': 'myapplication/json','charset' : 'utf-8'}
})
.then(function (res){
console.log(res.data)
},function (error){
console.log(error)
});

我尝试了很多事情,但都没有成功。

最佳答案

设置 header 和发送数据的顺序错误。首先设置 header ,然后将响应发送到前端。

res.setHeader('Content-Type', 'application/json');
res.send({ data: 'user created in db' })

其次,您的插入和查找是并行运行的。仅当在数据库中找不到用户时插入才有效

app.post('/signin',function (req,res) {
console.log('i recievied a get request in app.js = /signin');
console.log(req.body);
db.users.findOne({$or: [{username: req.body.username}, {email: req.body.email}]},function (err, data){
if (err) {
//Error Case (send to front end)
res.send( error)
} else {
if(data == null) {//Condition to check whether data exists or not
db['users'].insert({
username : req.body.username,
firstname : req.body.firstname,
lastname : req.body.lastname,
email : req.body.email,
password : req.body.password,
createdAt : new Date(),
role : "user",
approvedBy : req.body.approver.username,
status : "locked"
},function (err, data){
if ( err ) throw err;
res.setHeader('Content-Type', 'application/json');
res.send({ data: 'user created in db' })
});
} else {
res.send(data)
}
}
})
});

关于node.js - Nodejs表达如何将响应发送回浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50543069/

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