Uncaught(in promise)FirebaseError:应为类型“th”,但它是:自定义nh对象

z9smfwbn  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(167)

**我试图通过同时访问两个firebase firestore集合来处理此异步函数。但发生了以下错误。**第一个updateDoc函数完美地工作,但第二个函数给予。

const handleGiveAccess = async (uid, courseId) => {
        const reqDocRef = doc(db, `users/${uid}/sent_requests/${uid}_${courseId}`);
        updateDoc(reqDocRef, { enrollmentStatus: "resolved" })
          .then(() => {
            toast.success("Added to Resolved List! Refresh to see changes");
            console.log("Document successfully updated!");
          })
          .catch((error) => {
            console.error("Error updating document: ", error);
          });
    
        const requestsDocs = doc(db, `requests/${uid}_${courseId}`);
        updateDoc(requestsDocs, { enrollmentStatus: "resolved" })
          .then(() => {
            console.log("Enrollment status updated in requests collection!");
          })
          .catch((error) => {
            console.error("Error updating document: ", error);
          });
    
        const courseRef = doc(db, "courses", courseId);
        const totalEnrolledRef = collection(courseRef, "enrolled_list");
    
        const data = {
          uid,
        };
    
        setDoc(totalEnrolledRef, data)
          .then(() => {
            console.log("Document successfully written!");
          })
          .catch((error) => {
            console.error("Error writing document: ", error);
          });};
xkrw2x1b

xkrw2x1b1#

您的问题来自这些行:

const totalEnrolledRef = collection(courseRef, "enrolled_list");

setDoc(totalEnrolledRef, data)
  .then(() => {
    console.log("Document successfully written!");
  })
  .catch((error) => {
    console.error("Error writing document: ", error);
  });

在这里,totalEnrolledRef是一个CollectionReference对象。在下面的行中,您尝试使用CollectionReference而不是DocumentReference调用setDoc<T>(reference: DocumentReference<T>, data: WithFieldValue<T>): Promise<void>。您可能打算调用addDoc<T>(reference: CollectionReference<T>, data: WithFieldValue<T>): Promise<DocumentReference<T>>。您得到的错误消息将引用生产中使用的这些类的压缩名称。

const totalEnrolledRef = collection(courseRef, "enrolled_list");

addDoc(totalEnrolledRef, data) // <-- corrected here
  .then(() => {
    console.log("Document successfully written!");
  })
  .catch((error) => {
    console.error("Error writing document: ", error);
  });

**注意:**为了防止将来发生这种情况,您应该避免调用变量somethingRef。您应该使用somethingDocRef(对于DocumentReference对象)和somethingColRef(对于CollectionReference对象)。这将有助于在您查看代码时突出显示问题。
**注意:**您当前的代码中有大量的浮动promise。您应该确保在async函数中正确处理这些promise。您也可以考虑使用批处理写入(使用doc(totalEnrolledRef)生成新的DocumentReference来代替上面的addDoc步骤)。

相关问题