gpt4 book ai didi

javascript - React js : this. state.map 不是函数

转载 作者:行者123 更新时间:2023-12-01 01:52:30 25 4
gpt4 key购买 nike

我尝试解决我的问题,但无法解决。我使用 Express JS 创建了 API,我想显示(映射)我的状态数组。在 componentDidMount() 中,我更新 listOfUsers[] 和 console.log 返回正确的数据。当我想在 render() 中映射我的数组时,它会返回一些错误:

Uncaught (in promise) TypeError: this.state.listOfUsers.map is not afunction

Uncaught TypeError: this.state.listOfUsers.map is not a function

Warning: Can't call setState (or forceUpdate) on an unmountedcomponent. This is a no-op, but it indicates a memory leak in yourapplication. To fix, cancel all subscriptions and asynchronous tasksin the componentWillUnmount method.

enter image description here

import React, { Component } from 'react';
import axios from 'axios';

class MeetingItem extends Component {
state = {
meetings: [],
listOfUsers: []
}

componentDidMount() {
//get meeting info
axios.get(`http://localhost:3000/meetings/`+ this.props.match.params.id)
.then(res => {
const meetings = res.data;
this.setState({ meetings });
//console.log(res.data);
});

axios.get(`http://localhost:3000/takePart/`)
.then(res => {
for(var i = 0; i < res.data.length; i++){

//for current meeting
if(res.data[i]['meetingId'] == this.props.match.params.id){

//set listOfUsers
axios.get(`http://localhost:3000/users/`+ res.data[i]['userId'])
.then(res => {
const user = res.data;
this.setState({
listOfUsers : user
});

//in that place data is return
console.log(this.state.listOfUsers);
});
}
}
});
}

takePart = () => {
console.log("take");

const takePart = {
meetingId: this.props.match.params.id,
userId: sessionStorage.getItem('userId'),
};

//x-www-form
let formBody = [];
for (let property in takePart) {
let encodedKey = encodeURIComponent(property);
let encodedValue = encodeURIComponent(takePart[property]);
formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");

axios.post(`http://localhost:8000/takePart`, formBody, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
.then(res => {
console.log(res);
console.log(res.data);

//if successfully added a meeting then display alert
if(res.status == "200"){
alert("Thank you. See you later.");
}else{
alert("Sorry we can't handle that. Please repeat for a while.");
}
})
}


render() {
var users = this.state.listOfUsers.map(user => {
return (
<p>{user.firstName}</p>
)
});

return (
<div className="MeetingItem">
<h1>{/*this.props.match.params.id*/}</h1>
<p>Title: {this.state.meetings['title']}</p>
<p>Description: {this.state.meetings['description']}</p>
<p>Author: {this.state.meetings['author']}</p>
<p>lattitude: {this.state.meetings['lattitude']}</p>
<p>longitude: {this.state.meetings['longitude']}</p>
<p>date: {this.state.meetings['date']}</p>
<p>time: {this.state.meetings['time']}</p>

<p>{users}</p>

<div className="btn btn-default" onClick={this.takePart}>Take part</div>
</div>
);
}
}

export default MeetingItem;

有什么建议吗?

最佳答案

API 返回的用户列表应该是一个数组。

要处理此错误,您只需在映射之前检查 this.state.listOfUsers 是一个数组。

var users = Array.isArray(this.state.listOfUsers) && this.state.listOfUsers.map(user => {
return (
<p>{user.firstName}</p>
)
});

并确保 API axios.get('http://localhost:3000/users/'+ res.data[i]['userId']) 返回的用户列表是预期结果的数组。

关于javascript - React js : this. state.map 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51346479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com