0

My data in firebase looks like below:

/users
  --key1
    --cards
       --card1
         --name: X
         --age: 40
       --card2
         --name Y
         --age  45 
    --email: [email protected]

the code to query data looks like below

  this.userCardList = this.db.list<Card>('users', ref => ref.orderByChild('email').equalTo(email))

    return this.userCardList.snapshotChanges().pipe(
       map(changes => 
        changes.map(c => ({ key: c.payload.key, ...c.payload.val()}))
       )
      ) ;

i am invoking in my component.ts as below

 ngOnInit() {
    this.cards =   this.dataSvc.fetchUserCards(this.core.email).pipe(
      map((cards: any) => cards.map(cardObj => {
            var c = new Card(cardObj.key)

                  c.firstName = cardObj._firstName
                  c.lastName = cardObj._lastName
                  c.jobTitle = cardObj._jobTitle

                return c
        }))
    );
  }

The problem is that the above code is not mapped to cards node rather key1 node. if replace it as users/cards that does not work either.

My expectation is to be able to get cards node data rather the parent node.

5
  • So what is the actual output and what is the desired output? Commented Aug 30, 2019 at 11:45
  • also what does card1 and card2 contain... Commented Aug 30, 2019 at 12:15
  • i have updated the question to respond to your both questions Commented Aug 30, 2019 at 14:33
  • I want the cards node. So how should it look like? Do you mean cards = [{card1: {...}}, {card2: {...}}]? Commented Aug 30, 2019 at 17:06
  • Yes you are right Commented Aug 30, 2019 at 23:12

2 Answers 2

1

Okay in the comment I asked about the output, but realized now that the example I presented isn't valid. All cards should be wrapped inside an object if you would want an array. And it wouldn't really make sense to have one object of objects inside an array, So instead of an array with one item, you would get an object with all the cards, like so:

{ 
  "card1": {"name":"name1", ... },
  "card2": {"name":"name2", ... }
}

So first of all, you need to return the cards node that you want. Also you don't seem to want the key of the document(s) returned as per my comment, so let's omit that and only return the cards:

map(changes => 
  changes.map(c => ({ ...c.payload.val().cards }))
)

Then, just if in case you get more than one object listed from the query, let's apply all to a singe object:

this.cards = this.dataSvc.fetchUserCards(this.core.email).pipe(
  map(cards => return Object.assign.apply(null, cards))
);

This will give your desired output at the top of my answer.

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

Comments

0

so i decided to do it little differently. Now, i am doing it in 2 steps. First I am getting the key using the email as

 return new Promise((resolve, reject) => {
                  this.db.list("/users", ref => ref.orderByChild('email').equalTo(email)).snapshotChanges().pipe(
                    map(changes => 
                     changes.map(c => ({ key: c.payload.key, ...c.payload.val()}))
                    )
                   ).subscribe(
                    (res:any) => {
                        resolve(res)
                    },
                    err => {
                        console.log(err)
                        reject(err)
                    }
                  )}
                )

and then from the key i am fetching the cards array i need as below

 this.userCardList = this.db.list<Card>('users/' + key + '/cards', ref => ref.orderByChild('_status').equalTo(GlobalVariables.SYNCED))

    return this.userCardList.snapshotChanges().pipe(
       map(changes => 
        changes.map(c => ({ key: c.payload.key, ...c.payload.val()}))
       )
      ) ;

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.