简体   繁体   English

Console.log() 有一个值,但是当我将它与 map 函数一起使用时,它变得未定义 [ReactJS]

[英]Console.log() has a value but when I used it with map function it became undefined [ReactJS]

I'm very new to react.我很新来反应。

can you help me, please 😭你能帮帮我吗😭

I have spent a lot of time to research this error.我花了很多时间研究这个错误。 but I found nothing to solve my problem.但我发现没有什么可以解决我的问题。

I use console.log() it returns an array.我使用console.log()它返回一个数组。

but the next line, I used it with .map() function on line 30但是下一行,我on line 30它与.map()函数一起on line 30

it throws an error.它抛出一个错误。

TypeError: Cannot read property 'map' of undefined

this is my render() function这是我的render()函数

  render() {
    return (
      <div>
        {console.log("errrrrrrrrrrrrr",this.state.allowedRoutes)}
        {this.state.allowedRoutes.map(route => (
          <Route
            exact
            path={route.url}
            component={allRoutes[route.component]}
            key={route.url}
          />
        ))}
        {this.state.redirectRoutes.map(url => (
          <Redirect to={url} />
        ))}
      </div>
    );
  }

it logged the {console.log("errrrrrrrrrrrrr",this.state.allowedRoutes)} 's value.它记录了{console.log("errrrrrrrrrrrrr",this.state.allowedRoutes)}的值。

but It cannot use map() function.但它不能使用map()函数。

this is what my browser logged.这是我的浏览器记录的内容。

errrrrrrrrrrrrr (8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]

this is an error my browser:这是我的浏览器的错误:

一个错误

as error implies, this.state.allowedRoutes is undefined, either after logging the value or on subsequent renders.正如错误所暗示的那样, this.state.allowedRoutes是未定义的,无论是在记录值之后还是在后续渲染中。 you should also post your code too.你也应该发布你的代码。
But for a workarround you can check not to be undefined with this.state.allowedRouter && this.state.allowedRoutes.map()但是对于解决方法,您可以使用this.state.allowedRouter && this.state.allowedRoutes.map()检查是否undefined

As a side note, apparently you are updating your state in render method which is also not correct.作为旁注,显然您正在更新渲染方法中的状态,这也不正确。

It's likely that at some point you're updating your state and by mistake deleting allowedRoutes .很可能在某个时候您正在更新您的状态并错误地删除了allowedRoutes If you set your state without destructuring it first, all previous data will be removed, examples:如果您在不先解构的情况下设置状态,则将删除所有先前的数据,例如:

// WRONG - STATE BECOMES { allowedRoutes: myArray }
this.setState({
  allowedRoutes: myArray
});

// RIGHT - STATE BECOMES { all previous properties plus allowedRoutes: myArray }
this.setState({
  ...this.state,
  allowedRoutes: myArray
});

Also be sure to be initializing allowedRoutes as an empty array if you want to use map without checking the value first ( map is an array method).如果您想使用map而不先检查值( map是一个数组方法),请确保将 allowedRoutes 初始化为空数组。

If you're using classes:如果您正在使用类:

constructor(props) {
    super(props);
    this.state = {allowedRoutes: []};
}

If you're using hooks:如果您使用钩子:

const [allowedRoutes, setAllowedRoutes] = useState([]);

Your picture shows that the exception was thrown inside .map :您的图片显示异常是在.map中引发的:

this.state.allowedRoutes.map(
  // the exception was thrown here
  ...
)

Please notice at this line, you can see allRoutes is not defined in the current scope.请注意这一行,您可以看到allRoutes未在当前范围内定义。

component={allRoutes[route.component]}

暂无
暂无

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

相关问题 使用 forEach 映射对象数组的 Object.entries 返回 undefined 除非使用 console.log - Object.entries with forEach to map array of objects returns undefined except when console.log is used 当 map 函数在代码中时,它返回 undefined,当 console.log 时,它返回一个空对象和一个数据。 我只想要数据 - when map function is in the code it returns undefined and when console.log it returns the an empty object and an the data. i just want the data 为什么我在reactJs的console.log中变得未定义 - why am I getting undefined in console.log in reactJs 密码值返回未定义,但是当使用console.log显示时,用户可以看到 - Password value returns undefined, but when console.log is used to display it, the user can see it 为什么我console.log一个对象,它显示对象,但是当我console.log时,它显示未定义 - Why I console.log a Object,it shows object,but when I console Object.value ,it shows undefined 当我尝试在函数中添加一些结果时,为什么 console.log 打印到控制台“未定义”? - Why the console.log prints to the console "undefined" when I try to add some results in the function? 在 setInterval function 中使用时,console.log 运行无限次 - console.log when used in setInterval function is running infinite times console.log 当 onClick 在 ReactJs - console.log when onClick in ReactJs web 抓取 function 返回“未定义”,但在我使用 console.log 时有效 - web scraping function returns “undefined” but works when I use console.log 为什么当我 console.log() 时它说未定义? - Why does it say undefined when i console.log()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM