简体   繁体   English

UncaughtTypeError products.map 不是 function

[英]UncaughtTypeError products.map is not a function

product.map is not a function, i've tried almost all the fix from the web still not working. product.map 不是 function,我已经尝试了 web 的几乎所有修复程序仍然无法正常工作。 also, i've tried to console.log(response.data) and it response success另外,我尝试过 console.log(response.data) 并且它响应成功

react ver 7.0反应版本 7.0

constructor () {
        super()
        this.state = {
          products: []
        };
      }
  componentDidMount(){
         axios.get('http://localhost:8000/api/v1/products/1')
         .then(response => {
           this.setState({ products: response.data });
         })
         .catch(function (error) {
           console.log(error);
         })
       }
  render(){
   var { products } = this.state;
    var merk = products.map(products =>  {products.merk} );

got error products.map is not a function at var merk=.....得到错误产品。map 不是 function 在 var merk=.....

thanks.谢谢。

console.log:控制台日志:

{success: true, data: Array(1), message: "Phone retrieved Successfully.."}
data: Array(1)
0: {id_product: 1, merk: "Xiaomi ", tipe: "Mi Note 10", soc_nama: "Qualcomm", soc_tipe: "Snapdragon 855+", …}
length: 1
__proto__: Array(0)
message: "Phone retrieved Successfully.."
success: true
__proto__: Object

products.map is not a function , this mean products is not an array products.map is not a function ,这意味着products不是数组

In your API at componentDidMount, You have to access to the data that you return from server.在 componentDidMount 的 API 中,您必须访问从服务器返回的数据。 You accessing to response.data , this mean you just access to the data of XMLHttpRequest, not data of your API.您访问response.data ,这意味着您只是访问 XMLHttpRequest 的数据,而不是您的 API 的数据。

Try to console.log(response.data) , and see which one is the array data that you want to get from server尝试console.log(response.data) ,看看哪个是你想从服务器获取的数组数据

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM