0

I'm trying to insert data into my firebase JSon, which has the following structure

 {
      "gerechten" : [ {
            "id" : "1",
            "name" : "spaghetti",
            "description" : "Spaghetti bolognese met veel liefde gemaakt op grootmoeders wijze.",
            "kind" : "Pasta",
            "preparation" : "1)Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?",
            "img" : "../../assets/img/gerechten/spaghetti.jpg",
            "ingredients" : [ "5kg wortelen", "3 aardappelen", "1 komkommer", "2 eieren", "3 eetlepels bloem", "50g zout" ]
          } ]
    }

as you see, i got an array 'gerechten" and inside each array another array "ingredients". Right now i can add a items but i have no idea how to fill my "ingredients". Is there a way to let the user give inputs and with each ',' there is a new ingredient? Eq;

5 eggs, 2 appels, 1 spoon of suger

sould create

"ingredients" : ["5 eggs", "2 appels", "1 spoon of suger"]


Also, as of now I have to insert my id manually, how can I make it so it takes automatic the next ID? when I leave the ID empty I got this result: Firebase arror

Its does not put it inside an array

this is how i do it at the moment:

create.ts:

  gerecht = {
    description: "",
    kind: "",
    name: "",
    prepartion: "",
    ingredients: "",
    id: "",
    img: ""
  }

  create() {
    this.db.create(this.gerecht.id, this.gerecht.description, this.gerecht.kind, this.gerecht.name, this.gerecht.prepartion, this.gerecht.img, this.gerecht.ingredients) 
    this.navCtrl.push(HomePage)
  }

firedataservice:

  create(id: string, description: string, kind: string, name: string, preparation: string, img: string, ingredients: string) {
this.db.object('gerechten/'+id).update({
  id: id,
  description: description,
  kind: kind,
  name: name,
  preparation: preparation,
  img: img,
  ingredients: ingredients
})

}

Ramon
  • 123
  • 1
  • 5
  • I think you should read [This old blogpost](https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html) about arrays in firebase – André Kool Apr 11 '18 at 11:18
  • To split a single string `"5 eggs, 2 appels, 1 spoon of suger"`, use `split(",")`. See https://stackoverflow.com/questions/5269856/how-to-split-comma-separated-string-using-javascript – Frank van Puffelen Apr 11 '18 at 14:33

0 Answers0