简体   繁体   English

使用 ReactJS 获取数据不显示在浏览器上

[英]Fetch data keep not display on browser using ReactJS

I am creating a simple fetching data using ReactJS and MySql as my database.我正在使用 ReactJS 和 MySql 作为我的数据库创建一个简单的获取数据。 For connection between front end and the back, I am using express, everything is okay with array data appear in console.log but not in html page.对于前端和后端之间的连接,我使用的是 express,数组数据出现在 console.log 中,但没有出现在 html 页面中,一切正常。

I am added another package "node-fetch" but also not working.我添加了另一个 package “node-fetch”,但也无法正常工作。

Here is my code on App.js这是我在 App.js 上的代码

import React, { Component } from 'react';
import Navbar from '../src/components/Navbar';
import './App.css';
import fetch from 'node-fetch';

class App extends Component {
  constructor() {
    super();
    this.state = {
      tbl_barang: []
    };
  }

  componentDidMount() {
    fetch('http://localhost:4000')
      .then(res => res.json())
      .then(tbl_barang => this.setState({data: tbl_barang}, () => console.log('Customers fetched...', tbl_barang)));
  }

  render() {
    return (
      <div className='App'>
        <Navbar />
        <h1>Daftar Barang</h1>
        <ul>
        {this.state.tbl_barang.map(tbl_barang =>
          <li key={tbl_barang.id}>
            {tbl_barang.tipe_barang}
            {tbl_barang.product_name}
          </li>
        )}
        </ul>

        <button> Click disini </button>
      </div>
    );
  }
}

export default App;

and here is the code to get the data from MySQL这是从 MySQL 获取数据的代码

const path = require ('path');
const mysql = require ('mysql');

module.exports = function (app, connection) {
    //if no matching path, back to index.html
    app.get('/', function(req, res){
        connection.query('SELECT id, tipe_barang, product_name FROM tbl_barang', function (err, data){
            (err)? res.send(err):res.json ({data});
        })
    })
}

I expected the array data display on the html page我希望在 html 页面上显示阵列数据

It looks like you're setting state.data instead of state.tbl_barang .看起来您正在设置state.data而不是state.tbl_barang To fix this, change your componentDidMount() body:要解决此问题,请更改您的componentDidMount()主体:

componentDidMount() {
    fetch('http://localhost:4000')
      .then(res => res.json())
      .then(tbl_barang => this.setState({tbl_barang: tbl_barang.data}, () => console.log('Customers fetched...', tbl_barang)));
}

You could also update your component to use the state.data property instead, this depends on what name you want to use.您还可以更新您的组件以使用state.data属性,这取决于您要使用的名称。

Normally you would want to have two components, App and for the data the map function should point to an Item component, and you can pass the props down to the child.通常你会想要两个组件,App 和数据 map function 应该指向一个 Item 组件,你可以将道具传递给孩子。

So in your render change to...因此,在您的渲染更改为...

  render() {
   return (
   <div className='App'>
    <Navbar />
    <h1>Daftar Barang</h1>
    {this.state.tbl_barang.map(tbl_barang =>
      <TableItem key={tbl_barang.id} tbl_barang={tbl_barang} />
    }
    )}

Then make a TableItem component, I usually make a separate file for this component.然后制作一个 TableItem 组件,我通常为这个组件制作一个单独的文件。

const TableItem = ({ tbl_barang }) => {
    return (
        <div>
            {tbl_barang.tipe_barang}
            {tbl_barang.product_name}
        </div>
    )
}

export default TableItem;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM