Search code examples
javascriptnode.jsgun

Unable to create an object with array as a property


Node.js-Node.js, no browser, gun 0.8.8

I create an object

const body = { 
  a: {
    must: [
      { name: 'first', size: 1 } 
    ]   
  }
};
gun.get('watcher/f0de26c0-a29f-11e7-8661-154b982951a4').put(body);

Receive error

Invalid value at 'a.must'!

Server

const Hapi  = require('hapi');
const Gun   = require('gun');

const server = new Hapi.Server;
server.connection({ port: 8080 });
server.connections.forEach(c => Gun({ web: c.listener, file: 'data.json' }));
server.start();

Solution

  • Gun does not track arrays. You have several options to represent an array depending on whether you want elements themselves tracked or the array as a whole.

    1) Make an object that behaves somewhat like an array

    {
      0: 1,
      1: 134,
      2: "abc"
    }
    

    2) Stringify the array and store is as a propergy

    { val : '[1,134,"abc"]' }