使用Fetch的ReactJS消费REST API

时间:2018-10-03 05:08:48

标签: reactjs rest api

我的示例API反映如下:

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function (req, res, next) {
    var mysql = require('mysql')
    var connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: 'Abcd1234',
        database: 'mtest'
    });

    connection.connect()

    connection.query('SELECT name from catalog_product_flat_1', function (err, rows, fields) {
        if (err) throw err
        let products = rows[0].name;

        console.log('Product is: ', products)

        res.render(
            'product', { products: global.products, title: 'abc' }
        );

        connection.end()
    })
});

module.exports = router;

...因此我可以使用以下代码行显示crgName和crgStarteddatetime:

[
    {
        "$id": "1",
        "Id": "000-000-000",
        "crgName": "Walmart - 08/29/2018 - Standard ",
        "crgStarteddatetime": "2018-08-29T08:40:40",
        "crgScore": {
            "$id": "2",
            "Value": 171160002
        }
    },

]

...我的问题是(回望我的API):如何显示crgScore的ID和值?

1 个答案:

答案 0 :(得分:0)

    <ul>
                {audits.map(item => (
                  <li key={item.crg_buildingid}>
                    <b>Building Name</b>: {item.crgName} |<b>Start Date</b>: {item.crgStarteddatetime} |
 <b>crgScore ID</b> {item.crgScore['$id']} | <b>Value</b> {item.crgScore['Value']}

                  </li>

                ))}
                </ul>