1

Sorry for the stupid question but for the life of me I cant get this right and a bit of a noob. Im getting a frontend javascript error :

TypeError: Cannot read property 'data' of undefined

My code :

var getApiPipe = services.pipecall(_this.getPackage).success(function (data) {
                if (data.response[0].data.length > 0) {
                    _this.buyerObjectResponse = data.response[0].data;
                } else {
                    _this.noDataBuyer = true;
                }
                _thisRoot.loading = false;
            });

The JSON return which is coming back from server :

{
   "status":true,
   "response":[
      {
         "data":[
            {
               "ID":1,
               "auctionid":4,
               "sessionname":"Session 1",
               "created":"2017-09-25 10:13:57"
            }
         ]
      }
   ]
}

Please help :(

5
  • 1
    Use your debugger to figure out why it's undefined and what you're actually looking for. Commented Sep 25, 2017 at 19:01
  • 1
    My guess is that data may be a String Commented Sep 25, 2017 at 19:02
  • Since the error says "cannot read property 'data' of undefined, you need to examine for ".data", you need to check parent objects of this property. So, it seems that you are not able to get data.response[0] and probably the problem is with your "data" parameter. Somehow success callback is not able to have the value of data parameter. Commented Sep 25, 2017 at 19:07
  • The path is correct, check data type. Commented Sep 25, 2017 at 19:08
  • awesome thanks guys it seems that response.data works for some reason. Not sure might be that the object coming back under data isnt a complete array ? Commented Sep 25, 2017 at 19:12

1 Answer 1

3

First things first, make sure you're recieving the response type you're expecting!

After that, when looking at a possibly undefined nested property you can do a couple of thing, but the easiest for most people to understand is Duck Casing:

if(
  data &&
  data.response &&
  data.response.length &&
  data.response[0].data &&
  data.response[0].data.length
) {
  //do positive match here
} else {
  //no data
}

By checking for EACH part of what would be a positive match you can make sure that it will only go forward if each piece is there.

A slightly slower, much more generic possibility is to use a path function like you'd find in many libraries. Ramda has a pretty good implementation, but if you don't want to use Ramda, there is a vanilla JS version:

function path ( paths, object ) { // will traverse the path, and if prop is found, returns it.
  for(var i = 0, l = paths.length, intermediate = true; i < l && intermediate; i++){
    intermediate = object[paths[i]];
  }
  return intermediate !== undefined? intermediate : null;
}

This function will accept anything that is a relevant prop in an Object, including numbers for Array indexes.

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

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.