michael powers
michael powers

Reputation: 207

How to retrieve a document in Firebase using just the id

I have not been able to find a reference in the documentation on how to get a document reference when you know the id of it in firebase.

I am passing the id to a webpage to lookup a QR code. Rather than storing a secondary unique id for each qrcode document I am relying on the firebase unique id.

Here is the lookup I tried but which seems to fail.

firebase.firestore().collection('cues').doc(id).get().then(function (docsnapshot) {
        console.info('About: ' + docsnapshot.get('text'));
    });

Upvotes: 0

Views: 4403

Answers (2)

michael powers
michael powers

Reputation: 207

I was able to get my original code to work with this modification to the query

firebase.firestore().collection('cues').doc(id).get().then((doc) => {

... and then just use doc.get("field") to get values form my document

Upvotes: 3

MichelDelpech
MichelDelpech

Reputation: 863

you can access to data like this:

const { id } = docsnapshot
const data = docsnapshot.data()
const myDoc = { id, ...data } 

myDoc.text
myDoc.anything...

Upvotes: 1

Related Questions