0

I am using node request to access data on an api. Now when I use the following code to access the body I get the following response.

app.post("/", function(req, res){

//var series = req.body.series;
var circuits = req.body.Circuits;
var url = req.body.url

request("http://ergast.com/api/f1/circuits/" + circuits + ".json", function(error, response, body){
    console.log(body);
    res.write("<p>Here is the information about the circuit " + circuits + " in the " + url + " is about the circuit </p>")
  });
})

and I get the following response from the Api.

{
"MRData": {
    "xmlns": "http://ergast.com/mrd/1.4",
    "series": "f1",
    "url": "http://ergast.com/api/f1/circuits/brands_hatch.json",
    "limit": "30",
    "offset": "0",
    "total": "1",
    "CircuitTable": {
        "circuitId": "brands_hatch",
        "Circuits": [
            {
                "circuitId": "brands_hatch",
                "url": "http://en.wikipedia.org/wiki/Brands_Hatch",
                "circuitName": "Brands Hatch",
                "Location": {
                    "lat": "51.3569",
                    "long": "0.263056",
                    "locality": "Kent",
                    "country": "UK"
                }
            }
        ]
    }
  }
}

Now the question is that I need to access the properties. Like for instance, the url gives me an error of undefined in the paragraph tag. Any help would be appreciated.

4
  • body.MRData.url doesn't work? Commented Dec 30, 2018 at 20:21
  • @Pedro Silva That does not work. It still gives me undefined! Commented Dec 30, 2018 at 20:45
  • Check the typeof body like, console.log(typeof body), if it's string that you should use JSON.parse(body).MRData.url, if not I really can't understand your problem. Commented Dec 30, 2018 at 20:48
  • I am trying to access url or any of the properties in Circuits:[] . This is the response I am getting. TypeError: Cannot read property 'CircuitTable' of undefined Commented Dec 30, 2018 at 20:51

1 Answer 1

1

In your console.log it should be

console.log(JSON.parse(body)); 

Or

var bodyParsed = JSON.parse(body);
var yourUrl = bodyParsed.MRData.url

As I said in the comments, your body is a string, console.log(typeof body) returns string, so it needs to be parsed to an object with properties.

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

6 Comments

Suppose I wanted to access the url in this part of the json data "Circuits": [ { "circuitId": "brands_hatch", "url": "http://en.wikipedia.org/wiki/Brands_Hatch", "circuitName": "Brands Hatch", "Location": { "lat": "51.3569", "long": "0.263056", "locality": "Kent", "country": "UK" }
bodyParsed.MRData.CircuitTable should work like you requested.
var request = require('request'); request('ergast.com/api/f1/circuits/brands_hatch.json', function (error, response, body) { var bodyParsed = JSON.parse(body) console.log(bodyParsed.MRData.CircuitTable); }); That's my code, and it works like a charm, I don't understand how it can't work with you.
this is what I get with my paragraph tags Here is the information about the circuit aintree in the [object Object] series. res.write("<p>Here is the information about the circuit " + circuits + " in the " + yourUrl + " series </p>")
You have to do JSON.stringify(bodyParsed.MRData.CircuitTable) on your res.write. otherwise, it's impossible to convert from object to string.
|

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.