MOLEDesign
MOLEDesign

Reputation: 488

NODE.js unable to use restful data obtained by restler

I am using restler with NODE.JS to obtain data from a live feed. If I want to just display the whole feed to the screen it works beautifully, however I want to actually grab the feed and filter it down and only issue what is needed.

Here is the snippet of code from my API that works

app.get('/cis/public/:crs', function (req, res) {

    fullpath = 'http://(domain)/timetables/search_crs.php?crs='+req.params.crs;

    rest.get(fullpath).on('complete', function(result) {
        if (result instanceof Error) {
            console.log('Error:', result.message);
            this.retry(1000); // try again after 1 sec
        } else {
            res.send(result);
        }
    });

});

This displays the full feed on screen, though not formatted as I would expect.

This is an example of what I want to do, and this works fine if I am working with local JSON files.. example if I typed in something like

var results = JSON.parse(fs.readFileSync('./data/ppm.json', 'utf8'));

However when using restful in the following format I get a failure

app.get('/cis/public/:crs', function (req, res) {

    fullpath = 'http://(domain)/timetables/search_crs.php?crs='+req.params.crs;

    rest.get(fullpath).on('complete', function(result) {
        if (result instanceof Error) {
            console.log('Error:', result.message);
            this.retry(1000); // try again after 1 sec
        } else {
            var National = result.GetStationBoardResult.locationName;
            res.send(National);
        }
    });

});

The error that is thrown is

            var National = result.GetStationBoardResult.locationName;
                                                       ^
TypeError: Cannot read property 'locationName' of undefined

How can I use the data obtained from restler within node and not just for display on the screen.

Here is a sample (top part) of the returned JSON

{
  "GetStationBoardResult": {
    "generatedAt": "2015-03-18T10:49:36.9120348+00:00",
    "locationName": "Swindon",
    "crs": "SWI",
    "platformAvailable": true,
    "trainServices": {

Thanks

EDIT : Here is the final piece of code that now gives me useful and USABLE JSON to work with.

app.get('/cis/public/:crs/:total', function (req, res) {

    fullpath = 'http://(domain)/timetables/search_crs.php?crs='+req.params.crs+'&total='+req.params.total;

    rest.get(fullpath).on('complete', function(result) {
        if (result instanceof Error) {
            console.log('Error:', result.message);
            this.retry(1000); // try again after 1 sec
        } else {
            var Results = JSON.parse(result);
            res.send(Results);
        }
    });

});

I can now do extra things with Results. and break down the JSON into more usable (and filtered) info

Upvotes: 0

Views: 175

Answers (1)

Daniël Camps
Daniël Camps

Reputation: 1809

What happens when you console.log

result

Based on the error your object result does not have a property called

GetStationBoardResult

Edit: as the result was a string and not an object, it will be solved using:

var National = JSON.parse(result).GetStationBoardResult.locationName;

Upvotes: 1

Related Questions