2021年1月23日星期六

I can get all documents but unable to retrieve single document in Firestore

I am trying to retrieve a single document yet it indicates it does not exist, despite the fact when I run the logic to get ALL documents it is in the response

    const users = await db.collection('users').get();      users.forEach(user => {          console.log(user.id, user.data()) // <-- Works! displays ID...          const userRef = db.collection('users').doc(user.id)          if (userRef.exists) {              console.log("User exists")          } else {              console.log("User does not exist") // <-- Getting this though          }      })  

Its very weird, the below works if I hardcode the ID as a string:

async getUser() {      const id = "some-id-to-a-document"      const usersRef = fb.db.collection('users').doc(id);      const doc = await usersRef.get();      if (!doc.exists) {          console.log('No such document!');       } else {          console.log('Document data:', doc.data()); // <-- Getting this...      }  }  

But if I try to input an ID via the function...

async getUser(id) {      console.log(id) // <-- shows the ID!      const usersRef = fb.db.collection('users').doc(id);      const doc = await usersRef.get();      if (!doc.exists) {          console.log('No such document!'); // <-- Getting this...      } else {          console.log('Document data:', doc.data());      }  }  
https://stackoverflow.com/questions/65866556/i-can-get-all-documents-but-unable-to-retrieve-single-document-in-firestore January 24, 2021 at 10:13AM

没有评论:

发表评论