0

I have an instance where I am trying to query two separate tables within the same GET request and then return the results to objects that can be accessed by my rendered view for use. In my current setup, the SQL queries are returning the correct commands and the first query has the results (status) correctly returned and are accessible within my view, but I'm unable to return the second set of results (discoverySource returns undefined in my console log), which is likely due to the fact that javascript promises can only return a single value. Is there a better way to query or return both objects?

appRoutes.route('/') 

    .get(function(req, res){

        models.Status.findAll({
            where: {
                userId: req.user.user_id
            },
            attributes: ['statusId', 'statusDate', 'dataDateStart', 'dataDateEnd', 'title'],
            order: 'statusDate DESC',

        }).then(function(status,discoverySource){
            models.DiscoverySource.findAll({
                where: {
                    organizationId: req.user.organizationId
                },
                attributes: ['discoverySource']
            })
            return status;
        }).then(function(status, discoverySource){
            console.log(discoverySource);
            res.render('pages/app/high-level-activity-feed.hbs',{
                status: status,
                discoverySource: discoverySource,
                user: req.user
            });
        })
    })
cphill
  • 5,596
  • 16
  • 89
  • 182
  • Sounds like you're looking for `Promise.all` and `spread`? – Bergi Feb 17 '16 at 13:12
  • Please comment if the linked duplicate does not solve your problem. – Bergi Feb 17 '16 at 13:12
  • Please check my answer at here : http://stackoverflow.com/a/35458093/2767817 – gehlotparesh Feb 17 '16 at 13:30
  • Hey @gehlotparesh, I tried your answer, but I received this error at the first findAll call: `Error: Please note that find* was refactored and uses only one options object from now on.` `at Model.findAll (/Users/user/Desktop/Projects/node/synotate/node_modules/sequelize/lib/model.js:1335:11) at async.parallel.models.DiscoverySource.findAll.where.organizationId` – cphill Feb 18 '16 at 03:37
  • Hey cphill, I didn't change any code, I just updated the whole scenario with async module, all the queries are same as it was written in your question, however as per Bergi, it is not a good idea to use async with promises, he may be right as he may have got more knowledge, and sorry Bergi, I wasn't aware with the situation that async is not good with promises, as I haven't used promises, I have only used async, which I found to be the best, my intention was only to help cphill, but may be you are right and may be am wrong – gehlotparesh Feb 18 '16 at 05:25

0 Answers0