0

Just find it really hard to use Firestore to retrieve some information and use it.

Collection: University{
uniID: string,
average_Rating: number    
}

Collection: Review{
uniID: string,
Rating: number
userID: string,
}

Collection: User{    
userID: string
userName: string
university: string(uniID)    
}

Scenario: when the user wants to write a Review, the system assigns User's university to the review's uniID.

I could query the user's document and get its uniID, but how can I save the ID somewhere and use it when I need to?

I tried:

export class reviewServices {
    ...
    userUni: string;
    ...

    construcor(){
      this.userUni=""
     }




    public getUserInfo () {

        var userDocRef = this.afs.collection('users').doc(this.userId).ref;

        userDocRef.get().then(function(documentSnapshot) {

            this.userUni = documentSnapshot.data().uniID

    })

}

but it gives the error: Cannot set property 'userUni' of undefined TypeError: Cannot set property 'userUni' of undefined

why why why :(((

Efan Du
  • 123
  • 2
  • 4
  • 14
  • 2
    Define the callback as an arrow function: `userDocRef.get().then((documentSnapshot) => { ... }`. – ConnorsFan Oct 03 '18 at 02:28
  • but another question is, documentSnapshot.data().uniID is returning a Promise, I could not assign it to uniID as string, how can I solve this? – Efan Du Oct 04 '18 at 02:25

0 Answers0