gpt4 book ai didi

javascript - "Cannot read property ' map ' of undefined (React.js)"

转载 作者:行者123 更新时间:2023-11-30 14:02:40 24 4
gpt4 key购买 nike

我收到“无法读取未定义的属性‘map’”错误。我一直在查看人们发布的类似问题中提供的一些示例,但仍然没有运气。任何猜测

背景:我所在的 3 人团队正在开发一款应用程序,该应用程序允许用户查看群组中每个人在 map 上的位置。我的具体职责是尝试整合一个聊天界面。我决定使用 socket.io。

 import React, { Component } from 'react';
// import Chat from "./Chat";
// import logo from './logo.svg';
import FacebookLogin from 'react-facebook-login';
import GoogleLogin from 'react-google-login';
import Sidebar from "react-sidebar";


import { Map, InfoWindow, Marker, GoogleApiWrapper } from 'google-maps-
react';

import './App.css';
class App extends Component {
constructor(props) {
super(props);
this.state = {
sidebarOpen: true
};
this.onSetSidebarOpen = this.onSetSidebarOpen.bind(this);
}

onSetSidebarOpen(open) {
this.setState({ sidebarOpen: open });
}
render() {
const responseFacebook = (response) => {
console.log(response);
}

const responseGoogle = (response) => {
console.log(response);
}


return (
<div className="App">
<Sidebar
sidebar={<b>
<div
style={{ padding: 40 }}>
<br />
<FacebookLogin
appId=""
fields="name,email,picture"
callback={responseFacebook}
/>
<br />
<br />

<GoogleLogin
clientId=""
buttonText="Login with Google"
onSuccess={responseGoogle}
onFailure={responseGoogle}
/>
</div>
<div class="btn-group">
<button type="button" class="btn btn-dark dropdown-toggle" data-
toggle="dropdown" data-display="static" aria-haspopup="true" aria-expanded="false">
My Groups <i class="fas fa-users"></i>
</button>
<div class="dropdown-menu dropdown-menu-lg-right">
<button class="dropdown-item" type="button">Action</button>
<button class="dropdown-item" type="button">Another action</button>
<button class="dropdown-item" type="button">Something else here</button>
</div>
</div>

<br />
<br />
<hr />

<div class="btn-group">
<button type="button" class="btn btn-dark">
Current Group <i class="fas fa-users"></i>
</button>
<div class="dropdown-menu dropdown-menu-lg-right">
<button class="dropdown-item" type="button">Action</button>
<button class="dropdown-item" type="button">Another action</button>
<button class="dropdown-item" type="button">Something else here</button>
</div>
</div>
<br />
<i class="fas fa-user-circle fa-3x" style={{ marginRight: 5, marginTop: 10 }}></i>
<i class="fas fa-user-circle fa-3x" style={{ marginRight: 5, marginLeft: 10, marginTop: 5 }}></i>
<i class="fas fa-user-circle fa-3x" style={{ marginLeft: 5, marginTop: 10 }}></i>
<br />
<br />
<br />
<div className="messages">
{this.state.messages.map(message => {
return (
<div>{message.author}: {message.message}</div>
)
})}
</div>
{/* <form>
<div class="form-group">
<button style={{marginBottom: 10}} type="button" class="btn btn-dark">Chat <i class="far fa-comment-alt" style={{marginLeft: 5}}></i></button>

<textarea style={{padding: 10}} class="form-control"
id="exampleFormControlTextarea1" rows="3"></textarea>
</div>
</form> */}
</b>}
open={this.state.sidebarOpen}
onSetOpen={this.onSetSidebarOpen}
styles={{ sidebar: { background: "white" } }}
>
<button onClick={() => this.onSetSidebarOpen(true)}>
Open sidebar
</button>
</Sidebar>
<br />
<br />
<Map google={this.props.google} zoom={14}>

<Marker onClick={this.onMarkerClick}
name={'Current location'} />

<InfoWindow onClose={this.onInfoWindowClose}>

</InfoWindow>
</Map>
</div>

);
}
}



export default GoogleApiWrapper({
apiKey: `${process.env.REACT_APP_GOOGLE_MAPS_API_KEY}`
})(App)

最佳答案

这可能意味着状态没有被 messages 的键值填充。

您正在尝试映射 this.state.messages 的值,但似乎没有设置。

              {this.state.messages.map(message => {

如果 this.state.messages 未定义,那么您将无法在其上运行 .map(.map 是一个数组具体功能)。

您需要检查您的代码,以确保在尝试对其进行映射之前将 messages 设置为正确的状态。希望对您有所帮助。

关于javascript - "Cannot read property ' map ' of undefined (React.js)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56052065/

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