1

I'm trying to convert a js script to a node.js script. The JS script works fine, and managed to pull data from this URL and correctly use the information. However, when I try run it on Node.js, I get the error "Cannot read property of 'fixtures' undefined."I've added this package to pull the data, however I'm not sure why it's now throwing an error.

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();

//Get Data
var getJSON = function(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'json';
    xhr.onload = function() {
        var status = xhr.status;
        if (status === 200) {
        callback(null, xhr.response);
        } else {
        callback(status, xhr.response);
        }
    };
    xhr.send();
};

//Use Data
getJSON('http://api.football-data.org/v1/teams/343/fixtures',
function(err, data) {
    if (err !== null) {
    console.log("   An error has occured.")
    } else {
    var latest;
    for(x = 0; data.fixtures[x].status !== "TIMED"; x++) {
        latest = data.fixtures[x].matchday - 1;
    }
}};

The error is thrown at the line while(data.fixtures[x].status !== "TIMED"), and the error is

for(x = 0; data.fixtures[x].status !== "TIMED"; x++) {
               ^

TypeError: Cannot read property 'fixtures' of undefined

Please could you explain to me why JS can see the data and use it fine, but node.js sees the property to be undefined?

Many thanks.

EDIT: Removed legacy while loop that shouldn't have been there.

6
  • 1
    You have data in the code snippet, but fbdata in the error message. Maybe you mistyped the variable name? Commented Sep 27, 2017 at 13:31
  • Out of interest, why do you increment x and then reset it back to zero? Commented Sep 27, 2017 at 13:34
  • @mingos, good spot, but that was me just testing to see if Node didn't like data. Variable name is fine. Commented Sep 27, 2017 at 13:37
  • @Jamiec Actually, I told a lie in my previous comment. The while loop isn't needed in this code anymore. Commented Sep 27, 2017 at 13:38
  • Can we get the actual code you're using then? Commented Sep 27, 2017 at 13:40

2 Answers 2

1

The reason you're getting that message is because the variable data in your callback is undefined. The reason it is so is that you are passing it that

callback(null, xhr.response); //<-- You should have used responseText

But even that will not solve the issue - it is text not json. So parse it

callback(null, JSON.parse(xhr.responseText));
Sign up to request clarification or add additional context in comments.

Comments

1

The response property is part XMLHttpRequest Level 2, but xmlhttprequest only supports 1 at the moment:

Note: This library currently conforms to XMLHttpRequest 1.

For this reason xhr.response will be undefined.

Only xhr.responseText will be set, and this is a string so you would need to add the parsing of json yourself.

if (status === 200) {
  try { // use a try block here in case the the response is not parseable
     callback(null, JSON.parse(xhr.responseText));
  } catch(e) {
     callback(e)
  }
} else {

  callback(status, xhr.response);
}

1 Comment

I will try implement this in a couple of minutes when I'm back on my machine. Thanks in advance!

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.