Node.js TypeError:this.state.propertydetails.map不是函数
我正在尝试使用fetch将数据从mongodb数据库提取到我的reactjs前端,我一直在获取上面的错误。我也尝试过使用axios,但总是出现同样的错误。可能是什么问题?任何帮助都将不胜感激Node.js TypeError:this.state.propertydetails.map不是函数,node.js,reactjs,fetch,Node.js,Reactjs,Fetch,我正在尝试使用fetch将数据从mongodb数据库提取到我的reactjs前端,我一直在获取上面的错误。我也尝试过使用axios,但总是出现同样的错误。可能是什么问题?任何帮助都将不胜感激 import React, { Component } from 'react'; export default class App extends Component { constructor() { super(); this.state = { propertydet
import React, { Component } from 'react';
export default class App extends Component {
constructor() {
super();
this.state = {
propertydetails: []
};
}
componentDidMount() {
fetch('http://localhost:5000/property')
.then(res => res.json())
.then(propertydetails => this.setState({ propertydetails }, () => console.log('Data fetched', propertydetails)).catch(error => {
console.log(error)
}));
}
render() {
return (
<ul>
{this.state.propertydetails.map(propertydetail =>
<li key={propertydetail.id}>{ propertydetail.apartmentName}</li>
)}
</ul>
)
}
}
import React,{Component}来自'React';
导出默认类应用程序扩展组件{
构造函数(){
超级();
this.state={
propertydetails:[]
};
}
componentDidMount(){
取('http://localhost:5000/property')
.then(res=>res.json())
.then(propertydetails=>this.setState({propertydetails},()=>console.log('Data fetched',propertydetails)).catch(错误=>{
console.log(错误)
}));
}
render(){
返回(
{this.state.propertydetails.map(propertydetail=>
- {propertydeail.apartmentName}
)}
)
}
}
您确定API返回数组吗。您还可以添加保护条件。如果您可以尝试控制台记录api响应并检查api是否返回数组或对象,那就太好了
import React, { Component } from 'react';
export default class App extends Component {
constructor() {
super();
this.state = {
propertydetails: []
};
}
componentDidMount() {
fetch('http://localhost:5000/property')
.then(res => res.json())
.then(propertydetails => {
console.log('propertydetails',propertydetails)
this.setState({propertydetails});
})
.catch(e=>console.log('error', e);
}
render() {
return (
<ul>
{this.state.propertydetails && this.state.propertydetails.map(propertydetail =>
<li key={propertydetail.id}>{ propertydetail.apartmentName}</li>
)}
</ul>
)
}
}
import React,{Component}来自'React';
导出默认类应用程序扩展组件{
构造函数(){
超级();
this.state={
propertydetails:[]
};
}
componentDidMount(){
取('http://localhost:5000/property')
.then(res=>res.json())
。然后(propertydetails=>{
log('propertydetails',propertydetails)
this.setState({propertydetails});
})
.catch(e=>console.log('error',e);
}
render(){
返回(
{this.state.propertydeails&&this.state.propertydeails.map(propertydeails=>
- {propertydeail.apartmentName}
)}
)
}
}