如何将一个 mongodb 请求的结果同步传入下一个请求的值?
How do I pass in the result of one mongodb request synchronously into the value of the next request?
我需要将从第一个 MongoDB 查询中获得的 state.name 值放入下一组 MongoDB 查询中。当我 console.log 我的第一个查询时,我达到了预期的结果,但它在以下查询中显示为未定义。是它们是异步的还是我传错了?
我研究了在 Express + MongoDB 中将一个查询的结果传递到下一个查询的示例,并研究了 Promises - 不确定我是否完全理解它。
// GET State Home
router.get('/:stateid', ensureAuthenticated, (req, res) => {
const member = req.user;
console.log(member);
Promise.all([
state = States.findOne({url:req.params.stateid}),
statelegislation = StateLegislation.find({'state': state.name }),
statepolicy = StatePolicy.find({'state': state.name }),
stateregulation = StateRegulation.find({'state': state.name }),
statelitigation = StateLitigation.find({'state': state.name }),
]).then(([state,statelegislation,statepolicy,stateregulation,statelitigation]) =>
res.render('app/state/home', {
state:state,
statelegislation:statelegislation,
statepolicy:statepolicy,
stateregulation:stateregulation,
statelitigation:statelitigation,
}))
.catch(err => res.send('Ops, something has gone wrong'));
});
当在以下查询中传入字符串值而不是变量 state.name 时,我获得了该值所需的结果。
我无法从第一个 MongoDB 请求动态传递值。
感谢任何帮助!
你不能一次性完成 Promise.all
。在开始其余查询之前,您需要先等待 state
承诺:
router.get('/:stateid', ensureAuthenticated, (req, res) => {
const member = req.user;
console.log(member);
States.findOne({url:req.params.stateid}).then(state =>
Promise.all([
StateLegislation.find({'state': state.name }),
StatePolicy.find({'state': state.name }),
StateRegulation.find({'state': state.name }),
StateLitigation.find({'state': state.name }),
]).then(([statelegislation,statepolicy,stateregulation,statelitigation]) =>
res.render('app/state/home', {
state,
statelegislation,
statepolicy,
stateregulation,
statelitigation,
})
)
)
.catch(err => res.send('Ops, something has gone wrong'));
});
我需要将从第一个 MongoDB 查询中获得的 state.name 值放入下一组 MongoDB 查询中。当我 console.log 我的第一个查询时,我达到了预期的结果,但它在以下查询中显示为未定义。是它们是异步的还是我传错了?
我研究了在 Express + MongoDB 中将一个查询的结果传递到下一个查询的示例,并研究了 Promises - 不确定我是否完全理解它。
// GET State Home
router.get('/:stateid', ensureAuthenticated, (req, res) => {
const member = req.user;
console.log(member);
Promise.all([
state = States.findOne({url:req.params.stateid}),
statelegislation = StateLegislation.find({'state': state.name }),
statepolicy = StatePolicy.find({'state': state.name }),
stateregulation = StateRegulation.find({'state': state.name }),
statelitigation = StateLitigation.find({'state': state.name }),
]).then(([state,statelegislation,statepolicy,stateregulation,statelitigation]) =>
res.render('app/state/home', {
state:state,
statelegislation:statelegislation,
statepolicy:statepolicy,
stateregulation:stateregulation,
statelitigation:statelitigation,
}))
.catch(err => res.send('Ops, something has gone wrong'));
});
当在以下查询中传入字符串值而不是变量 state.name 时,我获得了该值所需的结果。
我无法从第一个 MongoDB 请求动态传递值。
感谢任何帮助!
你不能一次性完成 Promise.all
。在开始其余查询之前,您需要先等待 state
承诺:
router.get('/:stateid', ensureAuthenticated, (req, res) => {
const member = req.user;
console.log(member);
States.findOne({url:req.params.stateid}).then(state =>
Promise.all([
StateLegislation.find({'state': state.name }),
StatePolicy.find({'state': state.name }),
StateRegulation.find({'state': state.name }),
StateLitigation.find({'state': state.name }),
]).then(([statelegislation,statepolicy,stateregulation,statelitigation]) =>
res.render('app/state/home', {
state,
statelegislation,
statepolicy,
stateregulation,
statelitigation,
})
)
)
.catch(err => res.send('Ops, something has gone wrong'));
});