简体   繁体   English

如何从 firebase firestore(React js)读取数组文档字段

[英]How to read array document field from firebase firestore (React js)

Hi I am trying to get todo array field from cloud firestore database in a react project.嗨,我正在尝试从反应项目中的云 Firestore 数据库中获取 todo 数组字段。 When i print out with console log I can see the empty array symbol but It throws error at the same time and does not render the page.当我使用控制台日志打印出来时,我可以看到空数组符号,但它同时抛出错误并且不呈现页面。

在此处输入图像描述

This is the Firestore.js file to get data from firebase.这是从 firebase 获取数据的 Firestore.js 文件。

 export const userTodoList = async (email) => { await onSnapshot(doc(db, "users", email), (doc) => { console.log(doc.data().todos); return doc.data().todos; }); };

This the MainPage.js file to get the array and pass to CardList component.这是 MainPage.js 文件,用于获取数组并传递给 CardList 组件。

 const MainPage = () => { const todoArray = userTodoList(auth.currentUser.email); const [todos, setTodos] = useState(todoArray); return ( <div> <section> <NavBar></NavBar> <ToDoForm /> </section> <section> <CardList array={todos} /> </section> </div> ); };

This is the CardList component to render the cards with the array mapping.这是 CardList 组件,用于使用数组映射呈现卡片。 I am checking for if the array length is 0 or not but still get errors.我正在检查数组长度是否为 0 但仍然出现错误。

 const CardList = (props) => { const array = props.array; if (array.length === 0) { return ( <div className="grid_container"> <h2>Found no todos</h2> </div> ); } return ( <div className="grid_container"> {array.map((todo) => ( <Card title={todo.title} /> ))} </div> ); };

Errors.错误。

在此处输入图像描述 在此处输入图像描述

The object.map is not a function error occurs because the map method is not implemented on objects. object.map 不是 function错误,因为 Z1D78DC8ED51214E518B5114FE24 上的方法未实现。 To iterate over an object, use the Object.keys() method to get an array of the object's keys and call the map() method on the array of keys.要遍历 object,请使用Object.keys()方法获取对象键的数组并在键数组上调用map()方法。

Ie You can use Object.keys() or Object.entries() and then use map即您可以使用Object.keys()Object.entries()然后使用map

For example:例如:

 array.keys(obj).map(key => { //other statements });

For more details you can check this article有关更多详细信息,您可以查看这篇文章

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

相关问题 Firebase Firestore - 如何从特定文档中检索数组字段并将这些数组值用于 map 文档 - Firebase Firestore - how to retrieve an array field from a specific document and use those array values to map documents firebase firestore从document-js获取对象/数组 - firebase firestore get object/array from document - js 如何使用react从firestore中的firebase中删除文档? - how to delete a document from firebase in firestore by using react? 从 Firestore 文档中读取字段的最简单方法 - Easiest way to read field from Firestore document 从 Firebase 文档中读取数组 - Read array from Firebase Document React Firestore如何在加载时获取文档字段 - React Firestore how to get document field on load React Native 如何从 Firebase Firestore 获取数组数据? - React Native how to fetch Array data from Firebase Firestore? Firebase firestore 从字段数组中删除项目 - Firebase firestore remove item from array of field Firebase Firestore:如何更新或访问和更新 map、数组、文档、集合中的字段值 - Firebase Firestore: How to update or access and update a field value, in a map, in an array, in a document, that is in a collection 如何在包含文档中对象的对象数组中搜索特定字段并在 firestore (firebase) (JS) 中更新它们 - How to search for specific fields in an array of objects containing objects in a document and update them in firestore (firebase) (JS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM