mardi 29 mars 2016

Can't access to a specfic field in JSON, saying undefined in javascript

I have a project in Laravel + socket.io , I need to access a specific field in the json being broadcasted. Here is the code.

socket.js

redis.on('message', function(channel, message) {
    message = JSON.parse(message);
    io.emit(channel + ':' + message.event,message.data); 
});

In my client socket, I have this.

socket.on("comment-channel:App\\Events\\CommentEvent", function(message){
        alert(message.data);
 });

then this will successfully, alert this.

[{
    "id": 136,
    "content": "dffsadf",
    "user_id": "1",
    "task_id": "33",
    "created_at": "2016-03-29 10:47:19",
    "user": {
        "id": 1,
        "first_name": "Hulk",
        "last_name": "Hogan",
        "username": "",
        "email": " hulhogan@yahoo.com",
        "company_id": "1",
        "role_id": "0",
        "photo": "\/assets\/apps\/img\/photos\/lvrv5VOGRskwPHvFVakp.jpeg",
        "position": "asdfsadf",
        "phone": "+75843857834",
        "city": "",
        "country": "Singapore",
        "timezone": "",
        "created_at": "2016-03-10 04:16:24",
        "updated_at": "2016-03-10 07:54:12",
        "deleted_at": null
    }
}]

Then when I try this

alert(message.data.task_id);

or

alert(message.data['task_id']);

I get 'undefined'..

How can I access, the task_id?Thank You!!!!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire