简体   繁体   English

显示数据类型为 map 的 Firestore 数据:错误:对象作为 React 子项无效

[英]Display Firestore Data with a datatype of map: Error: Objects are not valid as a React child

Retrieving from firestore:从 Firestore 中检索:

const [product, setProduct] = useState([]);

  const getProducts = async () => {
    const querySnapshot = await getDocs(collection(db, "products"));
    const arr = [];
    querySnapshot.forEach((doc) => {
      arr.push({
        ...doc.data(),
        id: doc.id,
      });
    });
    setProduct(arr);
  };

  useEffect(() => {
    getProducts();
  }, []);

I have this colorMap retrieved from Firestore:我从 Firestore 中检索了这个colorMap

export const data = [
  {
    colorMap: { red: 6, blue: 7, green: 8 },
    id: "a4TK38mByQbim4TwOHMY"
  },
  
  {
    colorMap: { Black: 20, Gold: 10 },
    id: "m08YmrlFxhxLxyc3hVjT"
  },
  {
    colorMap: { green: 9, red: 19 },
    id: "nN2w57SiDovbtQ6EBzGb"
  }
];

How can I display it in the screen?我怎样才能在屏幕上显示它?

 {product &&
        product.map((index) => (
          <>
            <b>{index.prodName + "-" + index.size + index.cat}</b>
            <li>{index.id}</li>
          </>
        ))}

If I'll add this:如果我添加这个:

<li>{index.colorMap}</li>

It will cause an error:它会导致错误:

Uncaught (in promise) Error: Objects are not valid as a React child (found: object with keys {green, red, blue}) Uncaught (in promise) Error: Objects are not valid as a React child (发现: object with keys {green, red, blue})

How do I resolve this?我该如何解决这个问题? Any help would be appreciated.任何帮助,将不胜感激。 Thank you谢谢

You can use Object.entries() and then map those items as shown below:您可以使用Object.entries()然后使用 map 这些项目,如下所示:

{product &&
  product.map((index) => (
  <div>
    <b>{index.prodName + "-" + index.size + index.cat}</b>
    <p>{index.id}</p>
    <ul>
      {
        Object.entries(index.colorMap).map((color) => (
          <li>{color[0]} - {color[1]}</li>
        ))
      }
    </ul>
  </div>
))}

暂无
暂无

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

相关问题 React/FireStore:错误:对象作为 React 子项无效 - React/FireStore : Error: Objects are not valid as a React child 将用户数据添加到 Firestore 时出现“对象作为 React 子项无效” - "Objects are not valid as a React child" when adding user data to Firestore 如何通过反应从前端将 object 添加到我的 firestore? 不断收到错误 Uncaught Error: Objects are not valid as a React child - How can I add an object to my firestore from the frontend with react? Keep getting the error Uncaught Error: Objects are not valid as a React child 对象作为 React 子项无效 - firebase 数据库,axios - Objects are not valid as a React child - firebase database, axios 如何修复 Objects are not valid as a React child in next JS 13 and Next Auth? - How to fix Objects are not valid as a React child error in next JS 13 and Next Auth? 对象作为 React 子项无效(找到:object,键为 {}) - Objects are not valid as a React child (found: object with keys {}) Firebase Function、Firestore 和 Axios - 错误:参数“数据”的值不是有效的 Firestore 文档 - Firebase Function, Firestore, & Axios - Error: Value for argument "data" is not a valid Firestore document Cloud Firestore 文档添加给出错误“参数“数据”的值不是有效的 Firestore 文档。不能使用“未定义”作为 Firestore 值” - Cloud Firestore document add gives error "Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value" 无法嵌套 firebase/firestore 查询的 map 数据,React & Typescript - Unable to map data of nested firebase/firestore query, React & Typescript 错误:参数“数据”的值不是有效的 Firestore 文档。 不能将“undefined”用作 Firestore 值(在字段“chatId”中找到) - Error: Value for argument "data" is not a valid Firestore document. Cannot use "undefined" as a Firestore value (found in field "chatId")
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM