Yokesh Varadhan
Yokesh Varadhan

Reputation: 1636

how to get an item inside a response object

i am trying to get a value inside a response but i am getting null

enter image description here

this image is a response of an url but when i try to access it i am getting undefined

this.http.post(url, data, headers)
            .map(res => res.json())
            .subscribe(res => {
                console.log(res);
                console.log(res.devicePlaylist[0]);
                console.log("play list   :", res[1]);
            }, (err) => {
                console.log("failed", err);
            });

i an getting the res as object but i coulc not access devicePlaylist, could someone help me

Upvotes: 1

Views: 245

Answers (2)

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

Since you have printed the the res object as console.log(res) inside the subscribe method, check the browser console and notice that Object written there. This means that the value res is an object and it contains key : value pair. So when you try to access console.log(res.devicePlaylist[0]); then you will not find that printed in the console since res do not have a key named devicePlaylist. Thus, devicePlaylist is inside the key Response of res. So, you need to access it as,

res.Response.devicePlaylist[0]

And, since devicePlaylist is an array (also displayed as an array in the browser console), you can use the index value 0 until the array devicePlaylist contains at least one element. Otherwise, you will get undefined if the array is empty.

Upvotes: 1

abdoutelb
abdoutelb

Reputation: 1053

It seems that your outer object in console is the response ;can you try log res.Response.devicePlaylist[0]

Upvotes: 0

Related Questions