javascript - How do you find a document by its ID using version 9 of the Firebase JS SDK? - Stack Overflow

The new version 9 of the Firebase JS SDK has arrived with a more modular approach, and I'm trying

The new version 9 of the Firebase JS SDK has arrived with a more modular approach, and I'm trying to wrap my head around it.

I want to find and read a document from Firestore by its ID. In version 8, you could do:

await db.collection('users').doc(id).get();

Version 9 seems to have a function called "doc", and one called "getDoc", that could sound like the new equivalent, but I don't understand how to use them in practice. Has someone else played around with these yet?

Thanks in advance!

The new version 9 of the Firebase JS SDK has arrived with a more modular approach, and I'm trying to wrap my head around it.

I want to find and read a document from Firestore by its ID. In version 8, you could do:

await db.collection('users').doc(id).get();

Version 9 seems to have a function called "doc", and one called "getDoc", that could sound like the new equivalent, but I don't understand how to use them in practice. Has someone else played around with these yet?

Thanks in advance!

Share Improve this question asked Sep 1, 2021 at 10:43 The WormpleThe Wormple 831 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

The documentation has a plete code snippet explaining how to fetch a single document with given ID:

import { doc, getDoc } from "firebase/firestore";

const docRef = doc(db, "users", id);
const docSnap = await getDoc(docRef);

if (docSnap.exists()) {
  console.log("Document data:", docSnap.data());
} else {
  // doc.data() will be undefined in this case
  console.log("No such document!");
}

The doc() method is used to get a DocumentReference and getDoc() is used to fetch the document from given reference.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745370771a4624784.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信