1

I want to run 3 database query then render the 3 result objects to view, so I used async await to run queries first but seems its not waiting/working, always sending null objects to view before running queries. Cant find where I went wrong, I am using nodejs 12.16.1, not sure if its es6 supporting issue or sth else.

var express             = require('express');
var router              = express.Router();
var reviewModel         = require.main.require('./models/review-model');
var propertyModel       = require.main.require('./models/property-model');

router.get('/', async function(req, res){
    try{
        req.cookies['username'] == null ? loginCookie = null : loginCookie = req.cookies['username'];
    
        var getPromoteInfo = await propertyModel.getPromoteInfo(function(result){
            if(result!=null) return result;
        });

        var getPromoteReview = await reviewModel.getPromoteReview(function(result2){
            if(result2!=null)  return result2;
        });

        var getLatest3reviews = await reviewModel.getLatest3reviews(function(result3){
            if(result3!=null)  return result3;
        });

        res.render('index', {property:getPromoteInfo, rating:getPromoteReview, testimonials:getLatest3reviews, loginCookie});
    }
    catch(err){console.log(err);}
});

module.exports = router;

Model code:

var db = require('./db');

module.exports = {
    getPromoteInfo: function(callback){
        var sql = "select * from property where promote_status = 1;";
        db.getResult(sql, null, function(result){
            if(result){
                callback(result);
            }else{
                callback(null);
            }
        });
    }
}
2
  • 1
    Can you show us how e.g. getPromoteInfo is implemented? Apart from that the problem is probably that you're using both await and a callback-function. Commented Jun 20, 2020 at 13:55
  • 1
    just updated the question, i was thinking same, might be issue with await+callback Commented Jun 20, 2020 at 14:03

1 Answer 1

2

You're using await on a function that does not return a Promise resulting in an undefined value. So in order for async/await to work, you could rewrite getPromoteInfo as follows:

var db = require('./db');

module.exports = {
    getPromoteInfo: function(){
        return new Promise( (resolve, reject) => {
           var sql = "select * from property where promote_status = 1;";
           db.getResult(sql, null, function(result){
              if(result){
                resolve(result);
              }else{
                // you can decide whether to reject or not if no records were found
                reject();
              }
           });
        });
        
    }
}

In your express-handler you can then simply await this function call, without passing a callback:

const getPromoteInfo = await propertyModel.getPromoteInfo();

Note that you can check if your db-client/library supports promises out of the box - then you would not have to wrap your functions manually in a promise.

Sign up to request clarification or add additional context in comments.

1 Comment

didn't work without promise wrapper, saved my day :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.