0

I have a Firebase Realtime Database that looks like this:

Items:
    Object0:
        value1: "Foo-0"
        value2: "Bar-0"
    Object1:
        value1: "Foo-1"
        value2: "Bar-1"
    ...

... and so on, cut it down to simplify. Every "ObjectX" contains exactly the same structure, like value1, value2... The only difference between them is the values of value1, value2.

I need to get value1 from each of the objects. I can do that now by getting each object and assigning the values to a variable:

import { AngularFireDatabase } from '@angular/fire/database';
...
export class FooComponent{
  objectOne: ObjectModel = <ObjectModel>{};
  objectTwo: ObjectModel = <ObjectModel>{};

  constructor(private firebase: AngularFireDatabase) {}
  ngOnInit() {
    this.firebase
      .object('/Items/Object0')
      .valueChanges()
      .subscribe((objectm: ObjectModel ) => {
        this.objectOne = objectm; }

    this.firebase
      .object('/Items/Object1')
      .valueChanges()
      .subscribe((objectm: ObjectModel ) => {
        this.objectTwo = objectm; }

  valueOne = this.objectOne.value1; //"Foo-0"
  valueTwo = this.objectTwo.value1; //"Foo-1"
  }
}
export interface ObejctModel {
    value1: string;
    value2: string;   
  }

I imagine there is a way easier way to do this using firebase.list('/Items') instead of firebase.object, but I can't seem to find any solution that works for me. Can anyone help me refactor this code so it only uses 1 list instead of 2 (or more) objects?

Also, I must assign the values to variables in typescript, not the usual {{ items | async }} in the template approach.

1 Answer 1

3

You can do the following:

this.firebase.list('/Items').valueChanges().subscribe(items => {
     console.log(items);  
     items.forEach(values=>{ console.log(values) });
    });

The items will contain all the objects and the values in your database. You can also use forEach to iterate inside the array, that way you can access value1 and value2 without knowing the keys (Object0 and Object1).

Sign up to request clarification or add additional context in comments.

8 Comments

Yes, this works, but how do I then assign a value in items to a variable? I imagined something like this.SomeVariable = items.Object0.value1, but this doesn't even seem to be close to how it actually works.
what are you getting in the log? can you post the array that you get here in the comment
(2) [{…}, {…}] Object0: value1: "Foo-0" value2: "Bar-1" __proto__: Object Object1: value1: "Foo-1" value2: "Bar-1" __proto__: Object
I tried console.log(items[0]) and got only Object0, then I tried console.log(items[0][0]) and got undefined instead of "Foo-0"
what you can do also is to loop inside the array, so like this items.forEach(values=>{ console.log(values) }); you should have all the values inside the console.log
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.