【发布时间】:2020-04-24 08:02:31
【问题描述】:
我是 React 新手,它给了我下面的错误,我无法解决。我在网上搜索,很多人都遇到过同样的问题,但是“地图”是一个内置函数,为什么说它不是一个函数?
import React, { Component } from 'react';
export class UrunlerComponentList extends Component {
displayName = UrunlerComponentList.name
constructor(props) {
super(props);
this.state = { urunler_: [], loading: true };
fetch('http://localhost:55992/api/search/arama?q=&PageIndex=1')
.then(response => response.json())
.then(data => {
this.setState({ urunler_: data, loading: false });
});
}
static renderUrunlerTable(urunler_) {
return (
<table className='table'>
<thead>
<tr>
<th>SKU</th>
<th>Name</th>
</tr>
</thead>
<tbody>
{urunler_.map(urun =>
<tr>
<td>{urun.SKU}</td>
<td>{urun.Name}</td>
</tr>
)}
</tbody>
</table>
);
}
render() {
let contents = this.state.loading
? <p><em>Loading...</em></p>
: UrunlerComponentList.renderUrunlerTable(this.state.urunler_);
return (
<div>
<h1>Urunler</h1>
<p>This component demonstrates fetching data from the server.</p>
{contents}
</div>
);
}
}
我调用的 webapi 返回下面的值...
{
"$id": "1",
"RecordCount": 879,
"products": [
{
"$id": "2",
"Id": 17034,
"BrandId": 3,
"SKU": "7436B003-1082",
"Name": "xxxxxxxx",
"InStock": 1,
"URL": "xxxxxxxxxx",
"Description": "xxxxxxxxxxxxxxxxx",
"Price": 9.90,
"DiscountRatio": 0.00,
"TechnicalDetail": "xxxxxxxxxxxxxxxxx",
"ImageUrl": "7436Bxxxxxx.jpg",
"Active": true,
"CreatedDate": "2019-07-20T11:36:35.333",
"Brand": null,
"CartDetails": [],
"OrderDetails": [],
"ProductCategoryRelations": [],
"ProductColorRelations": [],
"ProductFileRelations": [],
"ProductPropertyRelations": []
},
【问题讨论】:
-
尝试在
renderUrunlerTable方法中下断点或检查urunler_的值,验证是否为数组?如果是数组,也请注明值。 -
你的数据不是一个数组而是一个对象
标签: javascript reactjs