Bendik Skarpnes
Bendik Skarpnes

Reputation: 3

How to get data from a referenced document in firebase?

In my data model i reference a user document from a "card" document.

1

I am struggeling to get the data of the user document using react.

onSnapshot(doc(firestore, "cards", cardId), (doc) => {
    const userDocument = doc.data().uid;
    console.log(userDocument);
});

This code logs an object without any data. Here is the object "gu":

gu:
    converter: null
    firestore: Su {type: "firestore", _persistenceKey: "[DEFAULT]", _settings: wu, _settingsFrozen: true, _app: FirebaseAppImpl, …}
    type: "document"
    _key: wt {path: Z}
    id: (...)
    parent: (...)
    path: (...)
    _path: (...)
    [[Prototype]]: Object

Upvotes: 0

Views: 735

Answers (1)

Tarik Huber
Tarik Huber

Reputation: 7398

A reference field type is "just" a reference. You can't get directly any data from it beside the path. The log you see is the same log that you would see if you would log any other reference in your code.

The only way to get data from that ref it so call get() ony it:

onSnapshot(doc(firestore, "cards", cardId), async (doc) => {
    const userDocument = doc.data().uid;
    userDocument.get().then(r=>{
      console.log(r.data());
    })
    
});

Upvotes: 2

Related Questions