Wednesday, 10 May 2017

How to handle node.js API type error

I am fairly new to node.js and am not sure how to handle this specific error. I am making an API call to get the inning and #batter of that inning using something like the following:

function (err, response, body) {
         // parse the body as JSON
         var parsedBody = JSON.parse(body);
         var i = 3;
         var b = 0;

         var inning = parsedBody.inning[i];
         var batter = inning[i].batter[b];
}

However when I iterate it will make a call for a number batter in that inning, which doesn't exist, I need to set i = i+1 and b=0, then begin iterating through b again until I hit that point once more. I am unsure of how to handle the error I get from the API call when the data doesn't exist. The error is as follows:

TypeError: Cannot read property 'firstName' of undefined
    at Request._callback (/Users/DrewGelinas/Desktop/App/app.js:41:100)
    at Request.self.callback (/Users/DrewGelinas/Desktop/App/node_modules/request/request.js:188:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (/Users/DrewGelinas/Desktop/App/node_modules/request/request.js:1171:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at IncomingMessage.<anonymous> (/Users/DrewGelinas/Desktop/App/node_modules/request/request.js:1091:12)
    at IncomingMessage.g (events.js:291:16)
    at emitNone (events.js:91:20)



via dgelinas21

No comments:

Post a Comment