I'm needing to place the obtained state.name value from my first MongoDB query into the next set of MongoDB queries. When I console.log my first query, I achieve the desired result, but it shows as undefined in the following queries. Is it that they are asynchronous or am I passing it in wrong?
I've researched examples of passing in results of one query into the next in Express + MongoDB as well as looked into Promises - not sure I quite understand it.
// 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'));
});
When passing in string values instead of the variable state.name in the following queries, I obtain the desired results for that value.
I am having trouble passing in the value dynamically from the first MongoDB request.
Any and all help appreciated!