0

I am using the following code to call an API and return results:

api.jobs.all(function(response) {

const obj = response.data.map(function(item) {
    return [item.id, item.billed.amountString];

});
});

With the following JSON:

{
  "data": [
  {
     "id": 2090170,
     "deadline": null,
     "jobId": {
        "id": 1644
     },
     "billed": {
        "amountString": 200,
        "currencyType": "CAD"
     }
    },
    {
     "id": 2090171,
     "deadline": null,
     "jobId": {
        "id": 1645
     },
     "billed": {
        "amountString": 400,
        "currencyType": "USD"
     }
}]}

The code is working fine, for the most part I am getting back good results, with the exception of: billed.amountString

I keep getting the following error:

TypeError: Cannot read property 'amountString' of null

Can anyone see why this would be returning null?

Also, is there a way in which I could loop through the API call and force it to do the following:

If .amountString === null, .amountString = "";
11
  • 2
    Your JSON is invalid. You have a single object in your array, which has two properties with the key "id". Commented May 2, 2017 at 22:14
  • Sorry, typo. I've updated the post. Commented May 2, 2017 at 22:15
  • 1
    You should re-check your JSON data, you have a single object inside a the data array, which contains duplicate properties, that I presume are supposed to be a part of a separate object in the array. Commented May 2, 2017 at 22:17
  • 1
    I've tried your code - no error Commented May 2, 2017 at 22:24
  • 1
    I even tried to add an additional id in the array where "amountString" is completely missing. console.table(json.data.map(function(i) { return [i.id, i.billed.amountString]; })); lists a table having no errors. Commented May 2, 2017 at 22:31

2 Answers 2

1

var response = {
  "data": [
  {
     "id": 2090170,
     "deadline": null,
     "jobId": {
        "id": 1644
     },
     "billed": {
        "amountString": 200,
        "currencyType": "CAD"
     }
    },
    {
     "id": 2090171,
     "deadline": null,
     "jobId": {
        "id": 1645
     },
     "billed": {
        "amountString": 400,
        "currencyType": "USD"
     }
}]};

const obj = (response.data).map(function(item) {
    return [item.id, item.billed.amountString];
});

console.log(obj);

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

1 Comment

I am glad that it resolved your problem. Thank you for asking this question. It helped me to brush up my concepts too :p
0

You could use the library lodash. The lodash method get can be used to try and access an object field. If it does not exist you can specify a default return value. See https://lodash.com/ .

// This will try to access item.billed.amountString
// If an item does not exist anywhere along the way
// it will return the default.

// _.get( OBJECT, PATH, DEFAULT )
_.get(item, ['billed', 'amountString'], '')

Comments

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.