06-18-2020 07:23
06-18-2020 07:23
Hello,
this an other basic question but still i have some troubles with sending an array from companion to my app...
in companion i m sending an array to app as so:
let myData = {voila : mesMoyennes,}
sendVal(myData);
function sendVal(data) {
if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
messaging.peerSocket.send(data);
}
then in app i m receiving the message using JSON as so :
if (evt.data.voila!=null)
{
// stringArray = (JSON.stringify(evt.data.voila));
// mesReves = JSON.parse(stringArray);
mesReves=`${JSON.stringify(evt.data.voila)}`
console.log ("is that right?" + mesReves);
console.log ("is that possible?" + mesReves[0].reve);
}
what is the right way to store a message after JSON transmitting it so i can access the index[i] of the array again?
what ever i do `${JSON.stringify (evt.data.myVar)]` does display the content of the array, but it still not the array i can use. wat is the proper way to do that.
thank you for answering !
Answered! Go to the Best Answer.
06-20-2020 15:29 - edited 06-20-2020 15:29
06-20-2020 15:29 - edited 06-20-2020 15:29
@Slumberlander wrote:Hello,
this an other basic question but still i have some troubles with sending an array from companion to my app...
...
what is the right way to store a message after JSON transmitting it so i can access the index[i] of the array again?
what ever i do `${JSON.stringify (evt.data.myVar)]` does display the content of the array, but it still not the array i can use. wat is the proper way to do that.
thank you for answering !
Hello @Slumberlander ,
JSON isn't really necessary in this case. You are going to receive an object (evt.data.voila). You can just assign it to a variable and use it.
pSok.onmessage=evt=>{
if (ex) return
if (kpay_common.isKPayMessage(evt.data)){console.log("kpay");return;}
if (!timeTest) tD = new Date()
cDate=tD.getTime()
var eD = evt.data.dR
...
You can also call a routine that will update your current variables with your new message:
var e={/* INITIALIZE COMM OBJECT(S) HERE*/}
update = (vals) => {
for (let x in vals) {
if (typeof e[x] === typeof vals[x]) e[x] = vals[x]
}
}
Then an "update(eD)" command will move your new comm values into a common object (in this case 'e') where it can be used as e['voila'] for example.
If you are using the file system, specifying a cbor or a json will take care of the stringifying and parsing for you. The only place I've needed to do this explicitly was with the settings storage API; you don't necessarily have to stringify a setting, but it works better if you do.
I believe the messaging system compresses to a cbor in transit, but the result is an object, perhaps a very complex object, but an object nonetheless.
Regards,
Reign
06-18-2020 08:16
06-18-2020 08:16
stringArray = JSON.stringify(evt.data.voila);
mesReves = JSON.parse(stringArray);
06-20-2020 15:29 - edited 06-20-2020 15:29
06-20-2020 15:29 - edited 06-20-2020 15:29
@Slumberlander wrote:Hello,
this an other basic question but still i have some troubles with sending an array from companion to my app...
...
what is the right way to store a message after JSON transmitting it so i can access the index[i] of the array again?
what ever i do `${JSON.stringify (evt.data.myVar)]` does display the content of the array, but it still not the array i can use. wat is the proper way to do that.
thank you for answering !
Hello @Slumberlander ,
JSON isn't really necessary in this case. You are going to receive an object (evt.data.voila). You can just assign it to a variable and use it.
pSok.onmessage=evt=>{
if (ex) return
if (kpay_common.isKPayMessage(evt.data)){console.log("kpay");return;}
if (!timeTest) tD = new Date()
cDate=tD.getTime()
var eD = evt.data.dR
...
You can also call a routine that will update your current variables with your new message:
var e={/* INITIALIZE COMM OBJECT(S) HERE*/}
update = (vals) => {
for (let x in vals) {
if (typeof e[x] === typeof vals[x]) e[x] = vals[x]
}
}
Then an "update(eD)" command will move your new comm values into a common object (in this case 'e') where it can be used as e['voila'] for example.
If you are using the file system, specifying a cbor or a json will take care of the stringifying and parsing for you. The only place I've needed to do this explicitly was with the settings storage API; you don't necessarily have to stringify a setting, but it works better if you do.
I believe the messaging system compresses to a cbor in transit, but the result is an object, perhaps a very complex object, but an object nonetheless.
Regards,
Reign
06-22-2020 01:09
06-22-2020 01:09
thank you reign !