最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firestore: Unable to read document data (Uncaught TypeError: docSnap.exists is not a function at HTMLFormElement) -

programmeradmin4浏览0评论

I am a new developer with no experience in Web apps. I have a collection called "users" in Firestore in which the document IDs are the user's emails. I am trying to read the data in one of the documents following the example provided by Firebase here

I get two errors:

  • First one is the use of "await": Uncaught SyntaxError: Unexpected reserved word.
  • Second is this message: Uncaught TypeError: docSnap.exists is not a function at HTMLFormElement.

I bypassed the first one by ommiting "await", but cannot avoid the second. Have you got any idea of what is wrong with my code?

console.log(docSnap) gives the following message: "Promise {pending}"

Thanks.

requestForm.addEventListener('submit', (event) => {
event.preventDefault();

const user = auth.currentUser;
const docSnap = await getDoc(doc(db, "users", user.email));

    if (docSnap.exists()) {
        console.log("Document data:", docSnap.data());
        } else {
        console.log("No such document!");
    }
})

I am a new developer with no experience in Web apps. I have a collection called "users" in Firestore in which the document IDs are the user's emails. I am trying to read the data in one of the documents following the example provided by Firebase here

I get two errors:

  • First one is the use of "await": Uncaught SyntaxError: Unexpected reserved word.
  • Second is this message: Uncaught TypeError: docSnap.exists is not a function at HTMLFormElement.

I bypassed the first one by ommiting "await", but cannot avoid the second. Have you got any idea of what is wrong with my code?

console.log(docSnap) gives the following message: "Promise {pending}"

Thanks.

requestForm.addEventListener('submit', (event) => {
event.preventDefault();

const user = auth.currentUser;
const docSnap = await getDoc(doc(db, "users", user.email));

    if (docSnap.exists()) {
        console.log("Document data:", docSnap.data());
        } else {
        console.log("No such document!");
    }
})

Share Improve this question edited Oct 11, 2021 at 19:07 Mount Vesubio asked Oct 11, 2021 at 18:01 Mount VesubioMount Vesubio 851 silver badge9 bronze badges 4
  • Try console.log(docSnap). I think the promise is not resolved – ABDULLOKH MUKHAMMADJONOV Commented Oct 11, 2021 at 18:06
  • Can you share the plete function? Is it an async function or no? – Dharmaraj Commented Oct 11, 2021 at 18:07
  • await can only be used in the context of an async function. The event listener isn't. – danh Commented Oct 11, 2021 at 19:09
  • @ABDULLOKHMUKHAMMADJONOV You are right: console.log(docSnap) gives "Promise {<pending>}" – Mount Vesubio Commented Oct 11, 2021 at 19:35
Add a ment  | 

3 Answers 3

Reset to default 13

Looks like you return a Promise and it does not meet the requirements of async/await. Try this way.

getDoc(doc(db, "users", user.email)).then(docSnap => {
  if (docSnap.exists()) {
    console.log("Document data:", docSnap.data());
  } else {
    console.log("No such document!");
  }
})

Be careful, getDoc vs getDocs

For single document

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

const docRef = doc(db, "cities", "SF");
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!");
}

For all document

import { collection, getDocs } from "firebase/firestore";

const querySnapshot = await getDocs(collection(db, "cities"));
querySnapshot.forEach((doc) => {
  // doc.data() is never undefined for query doc snapshots
  console.log(doc.id, " => ", doc.data());
});

Not work, the same code works some times and others it does not. I am guessing it has to do with initializeApp that can't be run twice. It's frustrating. It carries from back in the day, you might have to check if it already initialized

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论