我正在尝试在我的数据库(mongodb(中搜索一个用户/商店帐户,然后刷新登录页面但有一些错误,或者将其发送到他们所需的页面。
到目前为止,将它们发送到他们所需的页面作品,但是当试图res.redirect到/signin页面时,我会从节点控制台中获取此错误消息。
throw err;
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
这是实际的代码
//Rendering Signin Page
app.get('/signin', function(req, res) {
res.render('signin', {
err: false,
})
});
//Rendering Signin Page with err
app.get('/signin/err', function(req, res) {
res.render('signin',{
err: true,
})
});
app.post('/signin', function(req, res) {
let data = req.body;
User.find({email: data.email},'_id email password', function(err, docs) {
if(err) {
console.log(err);
}
else {
//Finding the matching user
for(i = 0; i < docs.length; i++) {
if(data.password == docs[i].password) {
res.redirect('/'+docs[i]._id + '/userhome')
}
}
if(docs.length === 0) {
console.log('no user found')
res.redirect('/signin/err');
return;
}
}
})
Shop.find({email: data.email}, '_id email password', function(err,docs) {
if(err) {
console.log(err);
}
else {
//Finding the matching user
for(i = 0; i < docs.length; i++) {
if(data.password == docs[i].password) {
res.redirect('/'+docs[i]._id + '/shophome')
}
}
if(docs.length === 0) {
console.log('no shop found')
res.redirect('/signin/err')
break;
}
}
})
})
这也是我要渲染的哈巴狗文件(我认为这不是问题(
doctype html
html
head
title uShop
//Bootstrap CSS
link(rel="stylesheet", href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous")
script(src='https://code.jquery.com/jquery-3.3.1.slim.min.js', integrity='sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo', crossorigin='anonymous')
script(src='https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js', integrity='sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1', crossorigin='anonymous')
script(src='https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js', integrity='sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM', crossorigin='anonymous')
body
nav(class="navbar navbar-expand-md navbar-dark bg-dark sticky-top")
a(class="navbar-text" style="font-size:175%; color:white; padding:0px" href="/") uShop.com
h1(class="display-2 text-center position-relative" style="top: 3rem; font-size: 400%") Sign In
form(action="/signin" method="POST")
div(class="form-row position-relative" style="top:7rem")
label(class="col-lg-4 col-form-label position-relative text-right" for="inputEmail" style="font-size: 150%; top:-5px; left: -5px;") Email:
input(class="form-control col-lg-4" type="email" name="email" id="inputEmail")
//- if err == true
//- div(class="invalid-feedback") email incorrect
br
div(class="form-row position-relative" style="top:7rem")
label(class="col-lg-4 col-form-label position-relative text-right" for="inputPassword" style="font-size: 150%; top:-5px; left: -5px;") Password:
input(class="form-control col-lg-4" type="password" name="password" id="inputPassword")
div(class="form-row position-relative" style="top:8rem")
input(class="btn btn-primary btn-lg offset-lg-4 " type="submit" value="Sign In")
app.post('/signin', function (req, res) {
let data = req.body;
User.find({ email: data.email }, '_id email password', function (err, docs) {
res.redirect('/' + ...);
})
Shop.find({ email: data.email }, '_id email password', function (err, docs) {
res.redirect('/' + ...);
});
})
res.redirect
在每个请求中不能两次运行,但是,从您的couse中,至少调用了两次
这个?
app.post('/signin', function (req, res) {
let data = req.body;
User.find({ email: data.email }, '_id email password', function (err, docs) {
// res.redirect('/' + ...);
Shop.find({ email: data.email }, '_id email password', function (err, docs) {
res.redirect('/' + ...);
});
});
})
另外,???
您需要重定向到
'/' + docs[i]._id + '/shophome'
和
'/' + docs[0]._id + '/userhome'
在 signin API
at onece(一个请求(?
我认为,HTTP(REST API(请求是不可能的, nodejs
和其他语言是相同的。
我认为,这是normal user
的目的。
您想在user
或shop
Collection中搜索。
- 添加
async/await
它将使您的代码更可读
app.post('/signin', async function(req, res) {
....
})
- 您需要在搜索参数中使用
findOne
进行集合搜索和使用email
和password
,因此无需进一步的密码检查
const userResult = await User.findOne({ email: data.email, password: data.password }, '_id email password');
const shopResult = await Shop.findOne({ email: data.email, password: data.password }, '_id email password');
- 作为
findOne
,如果email
和password
匹配,则返回object
。您可以检查两个结果是空的,然后重定向如下
if(!userResult && !shopResult) {
return res.redirect('/signin/err');
}
if(userResult) {
return res.redirect('/'+userResult._id + '/userhome')
}
if(shopResult) {
return res.redirect('/'+shopResult._id + '/shophome')
}
添加return
在触及redirect
上将确保执行代码在此时结束。
使用try/catch
记录不需要的错误。
async/await
的MND链接:https://developer.mozilla.org/en-us/docs/web/javascript/reference/reference/statement/statement/statements/async_function