0

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!

1 Answers1

0

You can't do it at once in a single Promise.all. You need to wait for the state promise first before starting the remaining queries:

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'));
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • This worked perfectly! The following queries were able to pull results with only the required values from the first. Thank you very much @Bergi. This is an **embedded** and not a **chained** promise, correct? – matiaskochlowski May 01 '19 at 20:29
  • Yes, nesting the promise handlers was the most simple approach here - but there are [many other ways to access previous promise results in a `.then()` chain](https://stackoverflow.com/q/28250680/1048572). – Bergi May 01 '19 at 20:39