0

Im using polymer and firebase and I was wondering how to create an array of objects inside an object.

Like the object above

I want nested data like in the object the groups single object whereby we have members and the names inside it

      this.$.query.ref.push({
        name: this.$.crewName.value,
        description: this.$.crewDescription.value,
        createddate: new Date().toString(),
        creator: this.$.createcrewlogin.user.uid,
        slug: sluggedname
      });

With a simple push method like this .How do I achieve that

Grimthorr
  • 6,856
  • 5
  • 41
  • 53
Tevin Thuku
  • 437
  • 1
  • 8
  • 19

1 Answers1

2

When you write to the Firebase Database from JavaScript, you pass in a standard JSON object. This means that you can just nest the objects inside the object you pass to push():

  this.$.query.ref.push({
    name: this.$.crewName.value,
    description: this.$.crewDescription.value,
    createddate: new Date().toString(),
    creator: this.$.createcrewlogin.user.uid,
    slug: sluggedname,
    subobject: {
      techpioneers: true,
      womentechmakers: true
    }
  });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • And What If I was to update the `suboject` object.. How would I do that – Tevin Thuku Jan 21 '17 at 05:46
  • That depends on what subobject you want to update. You'd first have to query to find that object's key and then `ref.child(key).child(subobject).update({ newkey: true, womentechmakers: false, techpioneers: null })`. This will add a new key, change `womentechmakers` to false and remove `techpioneers`. – Frank van Puffelen Jan 21 '17 at 05:52
  • Great.. I've understood – Tevin Thuku Jan 21 '17 at 06:16