简体   繁体   English

当我尝试将后端与 React 连接时,为什么它会返回“无法读取未定义的属性“地图”?

[英]Why does it return `Cannot read property 'map' of undefined` when I'm trying to connect backend with React?

I'm a beginner in React and I'm following this tutorial https://dev.to/kmaryam27/step-by-step-react-nodejs-and-mysql-simple-full-stack-application-2018-part-5-5a8c to connect my database with React in front end and display the data.我是 React 的初学者,我正在学习本教程https://dev.to/kmaryam27/step-by-step-react-nodejs-and-mysql-simple-full-stack-application-2018-part- 5-5a8c将我的数据库与前端的 React 连接并显示数据。 I kept getting TypeError: Cannot read property 'map' of undefined when running npm start at localhost:3000 in express-react/backend/client/src in part 5. Here is my express-react/backend/client/src/App.js:我不断收到TypeError: Cannot read property 'map' of undefined when running npm start at localhost:3000 in express-react/backend/client/src 在第 5 部分。这是我的 express-react/backend/client/src/App。 js:

 import React, { Component } from 'react'; import axios from 'axios'; import './App.css'; class App extends Component { state = { users:[] } componentDidMount(){ this.getUsers(); } getUsers = _ => { axios.get('/').then((data) => { console.log(data.data.users); this.setState({users: data.data.users}); }).catch(error => console.log(error)); } showUsers = user => <div key={user.id}>{user.username}</div> render() { const { users } = this.state; return ( <div className="App"> {users.map(this.showUsers)} </div> ); } } export default App;

I get Welcome to Express express at localhost:3001 when running npm start in /express/backend, and if I run node server as shown in tutorial part 3, I could see the database content in localhost:3000 .当在 /express/backend 中运行npm start ,我在localhost:3001收到 Welcome to Express express,如果我按照教程第 3 部分所示运行node server ,我可以在localhost:3000中看到数据库内容。

If I place a debugger above console.log(data.data.users);如果我在console.log(data.data.users);上方放置一个debugger , I could see variable data returns this: ,我可以看到变量data返回: 在此处输入图像描述

Could someone help please?有人可以帮忙吗?

data.data looks to contain a string instead of an object with the expected users key. data.data看起来包含一个字符串,而不是带有预期users密钥的 object。

You should make sure you reply within the backend with a valid JSON (currently, it's an HTML as string).您应该确保在后端使用有效的 JSON 进行回复(目前,它是一个 HTML 作为字符串)。

暂无
暂无

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

相关问题 反应我因为“无法读取未定义的属性&#39;地图&#39;”而发霉 - REACT i'm malding because of "Cannot read property 'map' of undefined" 尝试 map 我的道具时,我收到无法读取未定义错误的“练习”属性 - I'm getting a Cannot read property of 'exercises' of undefined error when trying to map my props TypeError: Cannot read property 'map' of undefined 当我尝试 map 通过从父组件传递到子组件的道具时显示 - TypeError: Cannot read property 'map' of undefined is showing when I'm trying to map over the props passed from parent component to child component 尝试访问嵌入的Google地图时,为什么会出现无法读取未定义的属性“ getCenter”的问题? - Why am I getting Cannot read property 'getCenter' of undefined when trying to access my embed google map? 尝试使用 react-leaflet-choropleth map choropleth 时出错:无法读取未定义的属性“地图” - Error when trying to map a choropleth using react-leaflet-choropleth: Cannot read property 'map' of undefined 我在我的 React 项目中收到此错误“TypeError: Cannot read property 'map' of undefined” - I'm getting this error “TypeError: Cannot read property 'map' of undefined” on my React project 尝试使用 function 的结果时出错,typeError: Cannot read property 'map' of undefined in React - Error when trying to use the result of a function, typeError: Cannot read property 'map' of undefined in React 未捕获的TypeError:无法读取未定义的属性&#39;ajax&#39;当我尝试将数据发送到url时 - Uncaught TypeError: Cannot read property 'ajax' of undefined When I'm trying to send data to url 为什么我的单元测试在这个 React 测试中返回“无法读取未定义的属性“模拟”? - Why does my unit test return 'cannot read property 'simulate' of undefined' in this React test? 类型错误:当我尝试访问 API 调用数据时,无法读取未定义的属性“映射” - TypeError: Cannot read property 'map' of undefined when I am trying to access API call data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM