4

I am building an Ionic app with Firebase, and I want my data to look like that:

-products
   |
   |-KJFokdxfKwEW_FJStGX
       |
       |-category: "cat1"
       |-description: "some description"
       |-name: "product name"
       |-price: 10
   |-KJFokdxfKwEW_FJsdjR
       |
       |-category: "cat2"
       |-description: "some description"
       |-name: "product name"
       |-price: 15

-categories
   |
   |-cat1
      |
      |-name: "cat1"
      |-products
         |
         |-KJFokdxfKwEW_FJStGX: true
         |--KJFojsdKwEW_FJsdDE: true
   |-cat2
      |
      |-name: "cat2"
      |-products
         |
         |-KJFokdxfKwEW_FJsdjR: true
         |-KJFojsdKwEW_FJUDjj: true

So the problem is when I want to add a new product to a category firebase generates a unique ID to look like this:

-cat1
          |
          |-name: "cat1"
          |-products
             |
             |-KJPvqd6zyg6OkoEf3mi
                |
                |-KJFojsdKwEW_FJsdDE: true
             |
             |-KJPvqXNnkksLG6a4RxW
                |
                |-KJFojsdKwEW_FJUDjj: true

In my code, I am using AngularFire, and that's how I am adding products:

function addProduct() {
      var products = $firebaseArray(refsFactory.products);

      products.$add(self.product).then(function (ref) {
        console.log("Product added with id :" + ref.key());
        var cat = $firebaseArray(refsFactory.categoryProducts(self.product.category));

        var prod = {};
        prod[ref.key()] = true;

        cat.$add(prod).then(function () {
          console.log("Category updated")
        }).catch(function (err) {
          console.log(err);
        })


      }).catch(function (err) {
        console.log("Error: " + err);
      });
    }

I want when the user adds a new product it should be added also in the 'categories' but without a new generated unique ID. So, I am asking if there is a solution to add a record in this manner.

kj007
  • 6,073
  • 4
  • 29
  • 47
zied hajsalah
  • 546
  • 1
  • 6
  • 17
  • I found the solution [here](http://stackoverflow.com/questions/24560075/setting-your-own-key-when-adding-an-object-to-firebase-angularfire/24581253#24581253) – zied hajsalah Jun 06 '16 at 07:56

0 Answers0