One question I have regarding the implementation in my Controller class involves the following line:
this.template.convertAndSend("/topic/greetings", message);
The 'message' being sent is an arrayList. In a JSP file, I establish a socket connection and subscribe to "topic/greetings". My goal is to access a specific field within the message body sent by the Controller. To achieve this, I have the following code:
var stompClient = null;
var socket = new SockJS('/gs-guide-websocket');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
stompClient.subscribe('/topic/greetings', function(message) {
console.log(message.body);
}
}
Upon executing this code, the following data is displayed on the browser's console:
[
{
"_id":{
"timestamp":1487493075,
"machineIdentifier":1371548,
"processIdentifier":-28904,
"counter":16629528,
"date":1487493075000,
"time":1487493075000,
"timeSecond":1487493075
},
"name":"Alex",
"age":25
}
]
My objective is to access the 'name' field of the object. However, when attempting message.body.name
, it returns undefined. I've also tried:
var obj = JSON.parse(message.body);
console.log(obj.name);
or
console.log(obj["name"]);
but unfortunately, they all result in undefined values.
UPDATE:
I came across a helpful link, but the mistake I made was trying:
var obj = message.body;
console.log(obj[0]);
instead of using:
var obj = JSON.parse(message.body);
console.log(obj[0].name);