简体   繁体   English

类型错误:无法读取未定义的属性(读取“onSnapshot”)

[英]TypeError: Cannot read properties of undefined (reading 'onSnapshot')

Im am trying to collect data from firebase database but get the error 'TypeError: Cannot read properties of undefined (reading 'onSnapshot')'我正在尝试从 firebase 数据库收集数据,但收到错误“TypeError:无法读取未定义的属性(读取“onSnapshot”)”

Feed.js Feed.js

    const Feed = ({profilePic, message, timestamp, username, image}) => {
    const [posts, setPosts]  =useState([]);
    //realtime database connection
     useEffect(() => {
     db.collection['posts'].onSnapshot(snapshot => (
    setPosts(snapshot.docs.map((doc) => ({ id: doc.id, data: doc.data() })))
   ));
    }, []);

   return (

Firebase.js Firebase.js

  import firebase from 'firebase/compat/app';
  import 'firebase/compat/firestore';

 //initialize Firebase
  firebase.initializeApp(firebaseConfig);

 //database
 const db = firebase.firestore();
 export default db;

The collection is a method, not an array. collection是一种方法,而不是数组。 So:所以:

db.collection('posts').onSnapshot(...
          // 👆      👆

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 × 类型错误:无法读取未定义的属性(读取“推送”) - × TypeError: Cannot read properties of undefined (reading 'push') 类型错误:无法读取未定义的属性(读取“过滤器”) - TypeError: Cannot read properties of undefined (reading 'filter') 未捕获的类型错误:无法读取未定义的属性(读取“然后”)? - Uncaught TypeError: Cannot read properties of undefined (reading 'then')? TypeError:无法读取未定义的属性(读取“主”) - TypeError: Cannot read properties of undefined (reading 'main') TypeError:无法读取未定义的属性(读取“toLocaleString”) - TypeError: Cannot read properties of undefined (reading 'toLocaleString') TypeError:无法读取未定义的属性(读取“setRestaurants”) - TypeError: Cannot read properties of undefined (reading 'setRestaurants') TypeError:无法读取未定义的属性(读取“国家”) - TypeError: Cannot read properties of undefined (reading 'country') TypeError:无法读取未定义的属性(读取“然后”) - TypeError: Cannot read properties of undefined (reading 'then') 未捕获的类型错误:无法读取未定义的属性(读取“8”) - Uncaught TypeError: Cannot read properties of undefined (reading '8') TypeError:无法读取未定义的属性(读取“问题”) - TypeError: Cannot read properties of undefined (reading 'ques')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM