[dss-developer] Getting "[Object object]" instead of Object values in Event parameters

Zi Yuan, Chan ziyuan at smartboxasia.com
Mon Jun 27 07:14:47 CEST 2016


Hello,

I would like to raise an issue with data conversion when attempt to query
data from server using event subscription. Here's a snippet of the code:

// client js (dss add-on script)
dss.ajaxSyncRequest('/json/event/subscribe', { subscriptionID: 12345, name:
'test' });

dss.ajaxAssyncRequestWithoutAnswer(
  '/json/event/get',
  { subscriptionID: 12345, timeout: 60000 },
  function(jsonString) { console.log(jsonString); }
);

// server js (dss event script)
new Event('test', { hello: 'world', foo: { bar: 'baz' }}).raise();

// result (jsonString value retrieve from client)
{
  "result": {
    "events": [{
      "name": "test",
      "properties": {
        "hello": "world",
        "foo": "[object Object]"
      },
      "source": {
        "set": ".zone(0).group(0)",
        "groupID": 0,
        "zoneID": 0,
        "isApartment": true,
        "isGroup": false,
        "isDevice": false
      }
    }]
  },
  "ok": true
}

As you can see, there is no problem getting variable 'hello', but unable to
retrieve variable 'foo'.

Of course, there's a workaround by storing the value object as a string
before passing over, and convert it back to object at the client (which is
what I am currently doing). But that doesn't fix the underlying issue of
not passing the value properly to the client.

// client js
dss.ajaxAssyncRequestWithoutAnswer(
  '/json/event/get',
  { subscriptionID: 12345, timeout: 60000 },
  function(jsonString) {
    var json = JSON.parse(jsonString);   // parse once
    var fooString = json.result.events[0].properties.foo;
    var foo = JSON.parse(fooString);     // parse twice
    console.log(foo.bar);
  }
);

// server js
var foo = { bar: 'baz' };
new Event('test', { hello: 'world', foo: JSON.stringify(foo) }).raise();



Regards,
Zi Yuan.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://forum.digitalstrom.org/pipermail/dss-developer/attachments/20160627/ea95c358/attachment-0001.html>


More information about the dss-developer mailing list