React,undefined不是对象 - json数据

时间:2021-03-15 01:57:16

I'm trying to loop through an array of data. I have followed a guide but unfortunately I'm not having the same success. The data file looks like this:

我正在尝试遍历一组数据。我遵循了一个指南但不幸的是我没有取得同样的成功。数据文件如下所示:

import React, {Component} from 'react';
export default  [
{ id: 1, lk:593458, ld:18033, status: 'Open'},
{ id: 2, lk:593388, ld:18036, status: 'Closed'},
{ id: 3, lk:593420, ld:18047, status: 'Open'}
]

and the file I'm trying to work with the data in like this:

和我正在尝试使用这样的数据的文件:

  import data from './data';

  const {data} = this.props;
  let markers = this.data.map(id => (

No matter what I get the error "undefined is not an object (evaluating 'this.data.map'. Where am I screwing up? Thanks

无论我得到什么错误“undefined不是一个对象(评估'this.data.map'。我搞砸了哪里?谢谢

2 个解决方案

#1


1  

You cannot re-declare an imported variable.

您无法重新声明导入的变量。

import data from './data';

const {data} = this.props;

The second line re-declares the data variable which you already imported from the ./data module.

第二行重新声明已从./data模块导入的数据变量。

You can either use the import x as y from './module syntax in order to rename your imported module or to not use the destructuring on line 2: const _data = this.props.data.

您可以使用'./module语法中的import x作为y来重命名导入的模块,或者不使用第2行的解构:const _data = this.props.data。

#2


0  

this.data would be undefined at that point?

那个时候这个数据是不确定的?

You can do:

你可以做:

data.map(e => {})

data.map(e => {})

#1


1  

You cannot re-declare an imported variable.

您无法重新声明导入的变量。

import data from './data';

const {data} = this.props;

The second line re-declares the data variable which you already imported from the ./data module.

第二行重新声明已从./data模块导入的数据变量。

You can either use the import x as y from './module syntax in order to rename your imported module or to not use the destructuring on line 2: const _data = this.props.data.

您可以使用'./module语法中的import x作为y来重命名导入的模块,或者不使用第2行的解构:const _data = this.props.data。

#2


0  

this.data would be undefined at that point?

那个时候这个数据是不确定的?

You can do:

你可以做:

data.map(e => {})

data.map(e => {})