boo boo - 3 months ago 7
AngularJS Question

How to access this json object

I'm using express js to send data from mysql. I send it using

res.json(theData)
.

In the client side I get it like this in the console:

{
"data":[
{
"PlazaID":1,
"PlazaName":"fff",
"PlazaAddress":"fff",
"PlazaContactNo":"45645",
"PlazaLanes":"34",
"PlazaStatus":"y",
"ClientID":1
},
{
"PlazaID":2,
"PlazaName":"plaza2",
"PlazaAddress":"p2",
"PlazaContactNo":"000",
"PlazaLanes":"2",
"PlazaStatus":"a",
"ClientID":2
}
],
"status":200,
"config":{
"method":"GET",
"transformRequest":[
null
],
"transformResponse":[
null
],
"url":"/getTollPlaza",
"headers":{
"Accept":"application/json, text/plain, */*"
}
},
"statusText":"OK"
}


I store these values in an array to populate a table using angularjs'
ng-repeat
like this:

for(i=0;i<response.data.length;i++){
tableArray.push({
plazaid:response.data[i].plazaid,
plazaname:response.data[i].plazaname,
plazaaddress:response.data[i].plazaaddress,
plazacontactnumber:response.data[i].plazacontactnumber,
plazalane:response.data[i].plazalane,
plazastatus:response.data[i].plazastatus,
clientid:response.data[i].clientid
});
}


When I console.log the array the values are undefined.

0: Object
clientid:undefined
plazaaddress:undefined
plazacontactnumber:undefined
plazaid:undefined
plazalane:undefined
plazaname:undefined
plazastatus:undefined

Answer

Try this. Please note that variable name are case sensitive.

for(i=0;i<response.data.length;i++){
    tableArray.push({
        plazaid:response.data[i].PlazaID,
        plazaname:response.data[i].PlazaName,
        plazaaddress:response.data[i].PlazaAddress,
        plazacontactnumber:response.data[i].PlazaContactNo,
        plazalane:response.data[i].PlazaLanes,
        plazastatus:response.data[i].PlazaStatus,
        clientid:response.data[i].ClientID
    });
}