1

I have looked at this question/answer but can't get it to work for my needs.

I have an async function in an offer.js file, which I need to call from a Controller file. The offer.js file works correctly and returns JSON data. It's the calling Controller file which I can't get to 'wait' for the data to come back before continuing with the rest of the page.

This is what the Controller file does:

var router = require('express').Router(); // Express.js router functionality
const Offer = require('../models/offer'); // the offer.js file which has getAllOffers() async function

// call/return the data from getAllOffers() async function
var rsOffersAll = async function() {
  return await Offer.getAllOffers(); 
}

// I would like rsOffersAll to have data before doing anything further at this point.

// if the homepage, supply the rsOffersAll data to the view.
router.get('/', function(req, res, next) {
  res.render('index', { data: rsOffersAll  });  // the data needs to be here. this should not run until the data is available to pass to the view
});

How do I ensure that var rsOffersAll has data before the router.get... stuff executes?

1
  • router.get('/', async function(...) { ... { data: await rsOffersAll () }); ...}); Commented Nov 17, 2019 at 20:44

2 Answers 2

3

rsOffersAll is redundant here, you await the result internally but you don't then await the result of rsOffersAll from the route handler.

router.get('/', async (req, res, next) => {
  const data = await Offers.getAllOffers();
  res.render('index', { data });
});
Sign up to request clarification or add additional context in comments.

9 Comments

thanks for this. but what if I want to do this res.render('index', { pagetitle: 'Homepage', metadesc: 'The site's homepage', data: rsOffersAll }); where I want to pass more parameters to the view than just the data ?
@volumeone just add them: { pagetitle: 'Homepage', /* ... */, data } (but just keep data, don't do data: rsOffersAll) Just having data is equivalent to having data: data.
@volumeone as blex has already explained, I've simply made use of short-hand notation, if you need more properties then you simply add them like you would normally
if I do this router.get('/', async (req, res, next) => { const data = await Offers.getAllOffers(); console.dir(data); res.render('index', { data }); }); How come data never prints to the console?
@volumeone yes, you would appear to be opening a global connection and not closing therefore when another request comes in it hangs
|
0

rsOffersAll is async and it will return a promise so you can use then

 router.get('/', function(req, res, next) {

    rsOffersAll().then((res) = >{
        res.render('index', { data: res  });
    });

});

3 Comments

I wouldnt recommend mixing Promises & async / await
@James can you explain the little bit reason for this ?
are you saying you agree with me but don't understand why? 🤔

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.