簡體   English   中英

我如何將數據從 Firestore 傳遞到 react-native 中的 function

[英]how can i pass data from firestore to a function in react-native

這是我想將數據傳遞給的 function

 function db(threads) {
    setThreads(threads);
    if (initializing) setInitializing(false);
  }

這是我對數據的查詢

useEffect(() => {
  firestore().collection('User').onSnapshot(querySnapshot => {
    const list = [];
    querySnapshot.forEach(doc => {
      const {  id, name  } = doc.data();
      list.push({
        _id: doc.id,
        id,
        name,
        // add this
        latestMessage: {
          text: '',
          userName: '',
          avatar: '',
        },
      });
    });
    db(list); });
}, []);

請幫幫我,我已經有一段時間了。

我使用最新版本的 react-native 和 firebase。

它一直向我顯示此錯誤

TypeError: null is not an object (evaluating '_useContext7.threads')

但是當我將它們記錄到控制台時,我可以看到那里數據庫中的所有數據。

我只需要threads.id和threads._id

Ciao,嘗試像這樣修改你的useEffect

useEffect(() => {
firestore().collection('User').onSnapshot(querySnapshot => {
const list = [];
querySnapshot.data().forEach(doc => {
  list.push({
    _id: doc.id,
    id: doc.id,
    name: doc.name,
    // add this
    latestMessage: {
      text: '',
      userName: '',
      avatar: '',
    },
  });
});
db(list); });
}, []);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM