【问题标题】:Failed to compile in the chrome browser in react jsreact js中chrome浏览器编译失败
【发布时间】: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(),而是括号之间的collection var)。然后是错字unSub大写'S'作为答案指出。

标签: reactjs firebase google-cloud-firestore


【解决方案1】:

从您的代码中,您显然缺少集合定义。不是useFiresore的参数吗? 关于 unSub,这是一个错字 unsub vs unSub

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 2016-04-16
  • 2021-06-14
  • 1970-01-01
  • 2014-08-08
相关资源
最近更新 更多