Tuesday 16 May 2017

NodeJS running a promise-mysql query inside a foreach loop waiting for result each time

I am trying to query a mysql database twice, the second time, multiple times for each result from the first time but I am unable to work out how to wait for the result from the second query before continuing the

 admin.get('/', function (req, res) {
        mysqlQ("SELECT I.ID as ID, C.ID AS CID, I.PD AS PostDate, C.Name AS CatName, U.UserName AS UserName, I.Title AS Title, I.Post AS PostData FROM categories AS C JOIN INAT AS I ON C.ID = I.Category JOIN Users AS U on U.ID = I.User").then(function (INAT) {
        INAT.forEach(function (team) {
            team.time = moment(team.PostDate).format("DD-MM-YYYY HH:mm:ss");
            team.TS = moment(team.time, "DD-MM-YYYY HH:mm:ss").fromNow();
            mysqlQ("SELECT I.ID AS InatID, J.Name AS JobTitle, C.Name AS Category FROM `Jobs` AS J JOIN Jobs2INAT AS J2I ON J.ID = J2I.JobsID JOIN INAT AS I ON I.ID = J2I.INATID JOIN categories AS C on C.ID = J.Categories WHERE I.ID = " + team.ID).then(function (jobs) {
                    team.jobs = jobs;
                });
        });
        var context = {
            INAT: INAT
        };
        var homeTemplate = pug.compileFile(__dirname + '/templates/home.pug');
        var html = homeTemplate(context);
        res.send(html);
    });
})



via bertie.io

No comments:

Post a Comment