简体   繁体   English

localhost:3000 和 Internet 上的生产服务器 (localhost:5000) 上的代码相同但行为不同

[英]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.

相关问题 为什么我不能在反应中使用 axios 从 localhost:3000 调用 localhost localhost:5000 - Why can't I call localhost localhost:5000 from localhost:3000 using axios in react 服务器和客户端具有相同的主机(本地主机),但端口不同(8080、3000)。 我可以使用 Access-Control-Allow-Credentials: true 吗? - Server and client have same host (localhost), but different ports (8080, 3000). Can I use Access-Control-Allow-Credentials: true? 我如何将 http post 请求从 localhost:5000 发送到 localhost:3000 - how can i send http post request from localhost:5000 to localhost:3000 节点:getaddrinfo ENOTFOUND (localhost:5000) 在不同的 localhost:8888 上运行 - node: getaddrinfo ENOTFOUND (localhost:5000) running on different localhost:8888 本地主机:3000 更改代码后不显示任何内容 - Localhost:3000 not displaying anything after changing code 使 localhost/... 弹出 localhost:3000/ - Make localhost/… popup localhost:3000/ 从localhost:3000 / admin到localhost:3000 - From localhost:3000/admin to localhost:3000 将 localhost:3000 重定向到 localhost:3000/newpage - Redirecting localhost:3000 to localhost:3000/newpage JS代码可在localhost上运行,但不适用于生产环境 - JS code working on localhost but not on production 我在本地主机与服务器上分配 object 有不同的行为 - I've got different behavior for object assign on localhost vs Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM