0

I want to check an element is exist in the json data for that am doing

 $.get('/geteststatus', getdata, function (data) {

          if (data != "" || data[0].status ) {

});

where getdata is unique id

am getting this error

TypeError: data[0] is undefined


if (data != "" || data[0].status) {}

json data contains only one row value.it does not contain multiple values.so i used data[0] checking

json data

{"id" : "12" , "status" : "GC", "_id" : ObjectId("52f3045873e7e96b18000005") }
Sush
  • 1,449
  • 8
  • 26
  • 51
  • what is the format of `data` if it is an object then try `data.status` instead of `data[0].status` – Arun P Johny Feb 06 '14 at 04:15
  • possible duplicate of [How can I debug my JavaScript code?](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Ram Feb 06 '14 at 04:16

3 Answers3

1

Since data is an object, you have to use

if(!data || data.status){
}
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Usually good to add an escape clause--just exit if you don't get any data.

$.get('/geteststatus', getdata, function (data) {
  if (!data or data.length === 0) { return; }
  // process data
});
SimplGy
  • 20,079
  • 15
  • 107
  • 144
0

you can also use typeof operator:

e.g.

if(typeof data.status != "undefined") {
   //do something
}
Dharmang
  • 3,018
  • 35
  • 38