[英]Same code but different behavior on localhost:3000 and on production server on the Internet (localhost:5000)
I am trying to learn React Js but I can't solve the error I am getting: One of my components work well on the local server Localhost:3000, but the same code shows an error on the production server localhost:5000 or at my domain on the Internet.我正在尝试学习 React Js,但无法解决出现的错误:我的一个组件在本地服务器 Localhost:3000 上运行良好,但相同的代码在生产服务器 localhost:5000 或我的互联网上的域名。
TypeError: this.state.students.map is not a function
at a.value (View.js:23)
at a.value (View.js:42)
at Vi (react-dom.production.min.js:187)
at Bi (react-dom.production.min.js:186)
at Wl (react-dom.production.min.js:269)
at Ou (react-dom.production.min.js:250)
at Cu (react-dom.production.min.js:250)
at xu (react-dom.production.min.js:250)
at vu (react-dom.production.min.js:243)
at react-dom.production.min.js:123
This is my code:这是我的代码:
import React, { Component } from 'react';
import Axios from 'axios';
import RecordsList from './RecordsList';
export default class View extends Component {
constructor(props) {
super(props);
this.state = { students: [] };
}
componentDidMount() {
Axios.get('http://pdp.1fortwo.com/php/list.php')
.then(response => {
this.setState({ students: response.data });
})
.catch(function (error) {
console.log(error);
})
}
usersList() {
return this.state.students.map(function (object, i) {
return <RecordsList obj={object} key={i} />;
});
}
render() {
return (
<div>
<h3 align="center">Users List</h3>
<table className="table table-striped" style={{ marginTop: 20 }}>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th colSpan="2">Action</th>
</tr>
</thead>
<tbody>
{this.usersList()}
</tbody>
</table>
</div>
);
}
}
The reason is because http://pdp.1fortwo.com/php/list.php
returns an empty response (as I saw here ) so when you call this.setState({ students: response.data });
原因是因为
http://pdp.1fortwo.com/php/list.php
返回一个空响应(正如我在这里看到的)所以当你调用this.setState({ students: response.data });
this.state.students.map
is left undefined and thus can't be mapped over. this.state.students.map
未定义,因此无法映射。
You need to find out why your backend is returning an empty response on production.您需要找出后端在生产中返回空响应的原因。
map() 是一种属于 Array 类型的方法,所以我猜当您在另一台服务器上时,您从 componentDidMount 中的 Axios.get 获得的响应不会返回 Array 类型。
My data in DB are utf-8 encoded what appeared to be a problem for json_encode function in my PHP file (list.php).我在 DB 中的数据是 utf-8 编码的,这似乎是我的 PHP 文件 (list.php) 中 json_encode 函数的问题。 When I added an option JSON_UNESCAPED_UNICODE to the function everything started to work just fine.
当我向函数添加一个选项 JSON_UNESCAPED_UNICODE 时,一切都开始正常工作了。
Thank you for your intention to help me!谢谢你愿意帮助我!
This is my list.php file:这是我的 list.php 文件:
$sql = "SELECT * FROM students";
//echo $sql;
$stmt = $conn->query($sql);
$cr=0;
$students = Array();
while($row = $stmt->fetch()) {
// echo $row['sid'];
$students[$cr]['sid'] = $row['sid'];
$students[$cr]['fName'] = $row['fName'];
$students[$cr]['lName'] = $row['lName'];
$students[$cr]['email'] = $row['email'];
$cr++;
}
echo json_encode($students, JSON_UNESCAPED_UNICODE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.