0

Here is the structure of my firebase database:

enter image description here

I am trying to retrieve a list of names only to send it via email by clicking a button.

The code I have so far:

  toemail(){
this.db.list('profiles').snapshotChanges().subscribe(
  res => {
    res.forEach(doc => {
      this.people.push(doc.payload.val());
    });
  });
  console.log(this.people);
}

This code adds all data (email, game, name) to the people list. How could I modify it to add only names of people who has game=1 to people list?

Thank you

1 Answer 1

1

I suggest you use start by creating a model for your data, as this will really help in keeping your code clean and readable as well as take advantage of linting Here is some modified code that might help

          toemail() {
            this.db.list('profiles').snapshotChanges().subscribe(
              res => {
                this.people = []; //Reset the array every time data changes
                this.people = res.filter(doc => {
                  let person = doc.payload.val() as Person; 
//Person is the data model. Although you can omit this part if you wish and the code will still work
                  return person.game === 1;
                });
              });
            console.log(this.people);
           }

Filter function takes an array as an input and returns a new array after looping through each entity, and only adds the entity to the new array if it satisfies a certain condition In your case, if person.game ===1 the entity is added

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

Comments

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.