Javascript 为什么我的布尔变量在路由中不改变它们的值?
我只是在为我的后端做一个get路由,我不明白为什么我的变量user和pass在console.log时仍然是false。除了findOne之外,还有其他方法/功能来检查用户名和密码是否正确吗Javascript 为什么我的布尔变量在路由中不改变它们的值?,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我只是在为我的后端做一个get路由,我不明白为什么我的变量user和pass在console.log时仍然是false。除了findOne之外,还有其他方法/功能来检查用户名和密码是否正确吗 app.get('/connect', (req, res) => { let user = false; let pass = false; User.findOne({login: req.query.login}).then((currentUser) => {
app.get('/connect', (req, res) => {
let user = false;
let pass = false;
User.findOne({login: req.query.login}).then((currentUser) => {
if (currentUser)
user = true;
})
User.findOne({password: req.query.password}).then((currentPassword) => {
if (currentPassword)
pass = true;
})
console.log(user); //prints still false
console.log(pass); //prints still false
});
您需要使数据库搜索异步。您可以使用async/await来实现这一点
app.get('/connect', async (req, res) => {
let user = false;
let pass = false;
const currentUser = await User.findOne({login: req.query.login});
if (currentUser)
user = true;
const currentPassword = await User.findOne({password: req.query.password});
if (currentPassword)
pass = true;
console.log(user);
console.log(pass);
});
您需要使数据库搜索异步。您可以使用async/await来实现这一点
app.get('/connect', async (req, res) => {
let user = false;
let pass = false;
const currentUser = await User.findOne({login: req.query.login});
if (currentUser)
user = true;
const currentPassword = await User.findOne({password: req.query.password});
if (currentPassword)
pass = true;
console.log(user);
console.log(pass);
});
看来等待的是决议 如上所述,由于异步性质,它将触发这些请求并立即继续。这就是为什么您的控制台将打印false,但在N次之后,它们实际上已更改 您可以通过以下方式将函数设置为异步:
async (a,b) => {}
如果你使用速记。然后,你可以说:等待函数调用;对于需要处理的ant异步调用
请记住,如果您想等待某些内容,则父函数需要是异步的。这才是真正的外卖
综合起来,给定您的代码如下:
app.get('/connect', async (req, res) => { // If you leverage await, you need to define parent function as async by a keyword.
let user = false;
let pass = false;
//you tell this function to wait until it has fully finished its promise chain.
await User.findOne({login: req.query.login}).then((currentUser) => {
if (currentUser)
user = true;
})
// Same as above
await User.findOne({password: req.query.password}).then((currentPassword) => {
if (currentPassword)
pass = true;
})
console.log(user); //now will print true.
console.log(pass); //now will print true.
});
我注意到上面的关键变化。似乎等待的是解决方案 如上所述,由于异步性质,它将触发这些请求并立即继续。这就是为什么您的控制台将打印false,但在N次之后,它们实际上已更改 您可以通过以下方式将函数设置为异步:
async (a,b) => {}
如果你使用速记。然后,你可以说:等待函数调用;对于需要处理的ant异步调用
请记住,如果您想等待某些内容,则父函数需要是异步的。这才是真正的外卖
综合起来,给定您的代码如下:
app.get('/connect', async (req, res) => { // If you leverage await, you need to define parent function as async by a keyword.
let user = false;
let pass = false;
//you tell this function to wait until it has fully finished its promise chain.
await User.findOne({login: req.query.login}).then((currentUser) => {
if (currentUser)
user = true;
})
// Same as above
await User.findOne({password: req.query.password}).then((currentPassword) => {
if (currentPassword)
pass = true;
})
console.log(user); //now will print true.
console.log(pass); //now will print true.
});
我注意到上面的关键更改。如果是异步的,则需要等待或嵌套它们。如果您不等待,它将调用调用并继续,立即转储false,但在请求完成后更改值。如果您让我知道这是否解决了您的问题,我可以写一些东西。如果它是异步的,您需要等待它们或嵌套它们。如果您不等待,它将调用调用并继续,立即转储false,但在请求完成后更改值。如果你让我知道这是否解决了你的问题,我可以写一些更好的解释完美的解释