【发布时间】:2021-12-31 01:10:53
【问题描述】:
我是 react js 和 firebase 的初学者。我正在关注一个 youtube 教程,其中作者创建了一个可以发布图像的应用程序,但它在这里显示了一个错误。
Line 10: 'collection' is not defined no-undef
Line 20: 'unSub' is not defined no-undef
Line 22: 'collection' is not defined no-undef
这是我正在关注的代码:
import { useState, useEffect } from "react";
import { projectFirestore } from '../firebase/Config';
const useFiresore = () => {
const [docs, setDocs] = useState([]);
useEffect(() => {
const unsub = projectFirestore.collection(collection)
.orderBy('createdAt', 'desc')
.onsnapshot((snap) => {
let documents = [];
snap.forEach(doc => {
documents.push({ ...doc.data(), id: doc.id});
});
setDocs(documents);
});
return () => unSub();
}, [collection])
return { docs };
}
export default useFiresore;
【问题讨论】:
-
错误似乎很明显; 此处未定义“集合” ->
.collection(collection)。collection是什么? (不是函数调用,collection(),而是括号之间的collectionvar)。然后是错字unSub大写'S'作为答案指出。
标签: reactjs firebase google-cloud-firestore