简体   繁体   English

未捕获的错误:对象作为 React 子对象无效(发现:object 和键 {todo})。 如果您打算渲染一组孩子,请使用数组

[英]Uncaught Error: Objects are not valid as a React child (found: object with keys {todo}). If you meant to render a collection of children, use an array

i'm doing a mern app currently and i got the following problem我目前正在做一个 mern 应用程序,我遇到了以下问题

I'm making a call to my API, and it gives me an array, and when i try to map over it, it gives an error我正在调用我的 API,它给了我一个数组,当我尝试通过它 map 时,它给出了一个错误

I'm receiving an array, and inside that array, there's another array inside, and i need the information of that array, but i can't use the map function我正在接收一个数组,在那个数组里面,里面还有另一个数组,我需要那个数组的信息,但是我不能使用 map function

Here i'm trying to map the array在这里,我正在尝试 map 阵列

import React from "react";

import { TodoMain, Todo, Close, CloseIcon, TaskContainer } from "./ToDoStyled";

import { useSelector, useDispatch } from "react-redux";

const ToDo = ({ setTodo, idRoom, setIdRoom }) => {
  const handleTodo = () => {
    setTodo(false);
    setIdRoom("");
    document.body.style = "overflow: auto";
  };

  const user = JSON.parse(localStorage.getItem("profile"));

  const todo = useSelector(state => state.tasks);

  console.log(todo);

  return (
    <div>
      <TodoMain>
        <Todo>
          <Close>
            <CloseIcon onClick={handleTodo} />
          </Close>
          <TaskContainer>
            {todo.map((task) => {
              return (
                <div key={task._id}>
                  <h1>{task.task}</h1>
                </div>
              );
            })}
          </TaskContainer>
        </Todo>
      </TodoMain>
    </div>
  );
};

export default ToDo;

And this is how the data looks like, i need to get to that information, but i don't know how这就是数据的样子,我需要获取这些信息,但我不知道如何

在此处输入图像描述

i've tried everything in these 5 hours, trying to change the structure of the data, triying to change the behavior in the backend so i can send the data to the DB in a different way, but this is literally so far the only way to receive the data, and i'm stuck at this:7我在这 5 个小时内尝试了一切,试图改变数据的结构,试图改变后端的行为,这样我就可以以不同的方式将数据发送到数据库,但这实际上是迄今为止唯一的方法接收数据,我被困在这个:7

the way you can access to that array, it's to map over it after mapping over the first array, here's how you would to it您可以访问该数组的方式,在映射到第一个数组之后,它是 map

import React from "react";

import { TodoMain, Todo, Close, CloseIcon, TaskContainer } from "./ToDoStyled";

import { useSelector, useDispatch } from "react-redux";

const ToDo = ({ setTodo, idRoom, setIdRoom }) => {
  const handleTodo = () => {
    setTodo(false);
    setIdRoom("");
    document.body.style = "overflow: auto";
  };

  const user = JSON.parse(localStorage.getItem("profile"));

  const todo = useSelector(state => state.tasks);

  console.log(todo);

  return (
    <div>
      <TodoMain>
        <Todo>
          <Close>
            <CloseIcon onClick={handleTodo} />
          </Close>
          <TaskContainer>
            {todo.map((data) => {
              const {task,_id} = data
              return (
                <div key={_id}>
                {task.map((stuff)=>{
                  const {todo,name,room} = stuff
                  return(
                <h1 style={{color: 'white'}} >{todo}</h1>
                  )
                })}
                  
                </div>
              );
            })}
          </TaskContainer>
        </Todo>
      </TodoMain>
    </div>
  );
};

export default ToDo;

暂无
暂无

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

相关问题 Uncaught Error:Objects are not valid as a React child (found: object with keys.If you meant to render a collection of children, use an array instead 未捕获的错误:对象作为 React 子项无效(已找到:带键的对象。如果您打算呈现子项集合,请改用数组 - Uncaught Error:Objects are not valid as a React child (found: object with keys.If you meant to render a collection of children, use an array instead 错误:对象作为 React 子项无效(找到:object,键为 {inputList})。 如果你打算渲染一个孩子的集合,使用一个数组 - Error: Objects are not valid as a React child (found: object with keys {inputList}). If you meant to render a collection of children, use an array 对象作为 React 子级无效(找到:object 和键 {children})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {children}). If you meant to render a collection of children, use an array instead 对象作为 React 子级无效(找到:object 和键 {value})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {value}). If you meant to render a collection of children, use an array instead 对象作为 React 子级无效(找到:object 和键 {weight})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {weight}). If you meant to render a collection of children, use an array instead 对象作为 React 子级无效(找到:object 和键 {_delegate})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {_delegate}). If you meant to render a collection of children, use an array instead 对象作为 React 子级无效(找到:object 和键 {arr})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {arr}). If you meant to render a collection of children, use an array instead 对象作为React子对象无效(找到:带有键{this}的对象)。 如果要渲染子级集合,请改用数组 - Objects are not valid as a React child (found: object with keys {this}). If you meant to render a collection of children, use an array instead 对象作为 React 子项无效(找到:object 和键 {totalItems})。 如果您打算渲染一组孩子,请改用数组 - Objects are not valid as a React child (found: object with keys {totalItems}). If you meant to render a collection of children, use an array instead 未捕获的错误:对象作为 React 子项无效(发现:[object Promise])。 如果您打算渲染一组孩子,请使用数组 instea - Uncaught Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instea
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM