TypeError:无法读取reactjs中未定义的属性'map'

安娜贝

我正在使用expressjs和reactjs开发一个应用程序。我使用expressjs从后端获取了数据,但得到了map is not a function

import React, { Component } from "react";
import "./products.css";
import Listofproducts from "./listofproducts";
import { Link } from "react-router-dom";

class products extends Component {
  constructor(props) {
    super(props);
    this.state = {
      productInfo: ""
    };
  }

  async getProducts() {
    try {
      const data = await fetch("http://localhost:4000/product");
      const jsonData = await data.json();
      this.setState({
        productInfo: jsonData
      });
      console.log(this.state.productInfo);
    } catch (error) {
      console.log(error);
    }
  }
  componentDidMount() {
    this.getProducts();
  }

  render() {
    return (
      <React.Fragment>
        <Listofproducts itemlists={this.state.productInfo} />
      </React.Fragment>
    );
  }
}

export default products;

这是组件productLists,我在其中发送了要使用的道具。

import React, { Component } from "react";
import Product from "./products";

class Listofproducts extends Component {
  render() {
    const { itemslist } = this.props;
    console.log(itemslist);
    // console.log is working here i get back the data on the console
    return itemslist.map(list => {
      console.log(list);
    });
  }
}

export default Listofproducts;
托勒

productInfo在的构造函数中将其设置为空字符串products,并且字符串没有map方法。

将默认值改为一个空数组,它将在网络请求完成之前和之后同样有效。

class Products extends Component {
  constructor(props) {
    super(props);
    this.state = {
      productInfo: []
    };
  }

  // ...
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ReactJS:TypeError:无法读取未定义的属性“ map”

TypeError:无法使用ReactJs读取未定义的属性“ map”

TypeError:无法读取未定义的Reactjs的属性“ map”

ReactJs-TypeError:无法读取未定义的属性“ map”

TypeError:无法读取未定义的Reactjs的属性“ map”?

无法读取ReactJS中未定义的属性“ map”

未捕获的TypeError:无法读取reactjs应用程序中未定义的属性'map'

未捕获到的TypeError:无法读取ReactJs中{Component} .render上未定义的属性“ map”

ReactJS 使用 Ionic Uncaught TypeError:无法从 axios 请求中读取未定义的属性“map”

ReactJs:使用地图浏览状态元素,TypeError:无法读取未定义的属性“ map”?

无法读取未定义的属性“ map”(ReactJS和AJAX)

TypeError:无法读取未定义的属性“ map”

TypeError:无法读取未定义的React Hooks的属性“ map”

反应TypeError:无法读取未定义的属性'map'

React Jest:TypeError:无法读取未定义的属性“ map”

无法读取未定义的TypeError属性“ map”

TypeError:无法读取未定义<Angular 8>的属性'map'

TypeError:无法读取未定义的React Native的属性'map'

`TypeError:无法读取功能组件中React中未定义的属性'map'

文本字段中的Reactjs物料加载defaultValue&无法读取未定义的属性'map'

类型错误:无法读取reactjs中未定义的属性“ map”

类型错误:无法在打字稿中读取未定义、reactjs 的属性“map”

TypeError:无法读取react-redux中未定义的属性'map'

TypeError:无法读取React Native中未定义的属性'map

如何修复'TypeError:无法在React中读取未定义的属性'map'

TypeError:无法读取未定义的属性“ map”-如何访问本地json API中的数组

如何修复JavaScript中的“ TypeError:无法读取未定义的属性'map'”?

TypeError:无法读取react.js中未定义的属性'map'

TypeError:无法读取Django中未定义的属性“ map”并做出反应