0

I quite new to Angular 5 and AngularFireList I downloaded this starter template

They use a state map in the todos folder where they have push the input value to the database. But I want to push data to the database with the value of input with it.

Here they push data:

 const ref = this.categoryService.categories().push({
     name: category.name,
     description: category.description,
     uid: '',
  });

 return ref.set({
         name: category.name,
         description: category.description,
         uid: ref.key,
});

And here is the this.categoryService.categories() method:

categories(): AngularFireList<Category> {
    return this.db.list(`/restaurants/${this.auth.auth.currentUser.uid}/restaurant_menu/`);
}

Than I create a pages folder my self where I load the categories like this:

categories$ = this.categoriesService.categories().valueChanges();

What I tried is to give the categroy.name as paramater to categories() like this:

const ref = this.categoryService.categories(category.name).push({
    name: category.name,
    description: category.description,
    uid: '',
 });

And than set the name to the service:

categoryName: any;
categories(category_name): AngularFireList<Category> {
    this.categoryName = category_name

    //HERE I WOULD LIKE TO USE THE CATEGORY_NAME TO NEST IT IN RESTAURANT_MENU

    return this.db.list(`/restaurants/${this.auth.auth.currentUser.uid}/restaurant_menu/${category_name}`);
}

Than write a function to retrieve the value in the category service like this:

getCategoryName(){
   return this.categoryName;
}

And than in the pages folder call the getCategoryName method

categoryName: any = this.categoriesService.getCategoryName();

categories$ = this.categoriesService.categories(categoryName).valueChanges();

But this returns me undefined could someone clear this out or show me how this is done in best practise?

Sireini
  • 4,142
  • 12
  • 52
  • 91
  • You need to subscribe to `valueChanges()` to get the values. https://stackoverflow.com/a/48505041/1350162 – callback Mar 27 '18 at 07:23
  • so what you say is smth like this: `categoryName: any = this.categoriesService.getCategoryName();` `categories$ = this.categoriesService.categories(categoryName).valueChanges().subscribe(data=>{ console.log(data); })` – Sireini Mar 27 '18 at 08:22
  • But the categoryName I want to query is undefined... – Sireini Mar 27 '18 at 08:31
  • Yes, so you seem to be assigning `this.categoryName = category_name` in that `categories` method, but I dont see you calling it anywhere? – callback Mar 27 '18 at 08:34
  • Also, you might have to use a subject (http://reactivex.io/rxjs/manual/overview.html) if that value is going to keep changing, and you want to always keep getting the latest value. – callback Mar 27 '18 at 08:36
  • @callback yes I do at the last code block categoryName = this.categoriesService.getCategoryName(); And the value is going to keep changing is there a way to just call them all, to show them all. – Sireini Mar 27 '18 at 08:38
  • Yes, I see that, but how about `categories()` ? It is the one where you **set** the categoryName. I dont see it being called anywhere? – callback Mar 27 '18 at 08:41
  • In the categories$ var – Sireini Mar 27 '18 at 10:09
  • Well when you are calling `categoryName: any = this.categoriesService.getCategoryName();` the first time, `this.categoryName` is still undefined! – callback Mar 27 '18 at 10:20
  • @callback so I have to call `categories()` before the `getCategoryName()`? – Sireini Mar 27 '18 at 11:40
  • @callback could you please help me out – Sireini Mar 27 '18 at 16:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167676/discussion-between-callback-and-sreinieren). – callback Mar 27 '18 at 21:30
  • @callback are you available now? – Sireini Mar 28 '18 at 16:49
  • yes I am for a bit! – callback Mar 28 '18 at 19:10

0 Answers0