Rob
Rob

Reputation: 43

Struggling to get callback to work express.js

Trying to make sure that request has completed before rendering page

Overview of app - submit for with code, make request, populate results page

//Index.js

var response = require('./requestMapping')

// home search page
exports.index = function(req, res){
  res.render('index', { stationDisplay: response.station_id, test: 'rob' });
};

**//post from form to call METAR service
exports.post =('/', function(req, res, next) {
  response.getMETAR(req.body.query,function(){
    res.render('results', {
        stationDisplay: response.station_id,
        test: 'rob'
    });
  });
})**

//Index.ejs

<!DOCTYPE html>
<html>
  <head>
    <title><%= stationDisplay %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <h1>Enter ICAO code to get the latest METAR</h1>
    <form method="post" action="/">
        <input type="text" name="query">
        <input type="submit">
    </form>
  </body>
</html>

Module to call webservice - requestMapping.js

/**
 * @author robertbrock
 */
//Webservice XML
function getMETAR(ICAO){

    var request = require('request');
    request('http://weather.aero/dataserver_current/httpparam?datasource=metars&requestType=retrieve&format=xml&mostRecentForEachStation=constraint&hoursBeforeNow=24&stationString='+ICAO, function(error, response, body){
        if (!error && response.statusCode == 200) {
            var XmlDocument = require('xmldoc').XmlDocument;
            var results = new XmlDocument(body);

            console.log(body)
            console.log(ICAO)
            exports.station_id = results.valueWithPath("data.METAR.station_id");
//etc.

        }
    })
}
exports.getMETAR =getMETAR;

Upvotes: 1

Views: 941

Answers (2)

NilsH
NilsH

Reputation: 13821

I can't see that your getMETAR function actually takes a callback function? I would expect it to be:

function getMETAR(ICAO, callback) {
    // Do your work
    var station_id = results.valueWithPath("data.METAR.station_id");
    callback(null, station_id); // It's common to use the first arg of the callback if an error has occurred
}

Then the code calling this function could use it like this:

app.post('/', function(req, res) {
    response.getMETAR(req.body.query, function(err, station_id) {
        res.render('results', {stationDisplay: station_id, test: 'rob'};
    });
});

It takes some time getting used to async programming and how the callbacks work, but once you've done it a few times, you'll get the hang of it.

Upvotes: 1

generalhenry
generalhenry

Reputation: 17319

Without seeing the rest of your code it hard to guess, but your code should look more like:

app.post('/', function(req, res, next) {
  response.getMETAR(req.body.query,function(){
    res.render('results', {
        stationDisplay: response.station_id,
        test: 'rob'
    });
  });
});

Upvotes: 0

Related Questions