从节点获取api数据

问题描述 投票:0回答:3

我与Node JS上的数据库具有odbc连接。

var express = require('express');
var odbc = require('odbc');
var app = express();
var connectionString = 'Dsn=xxx;uid=xxx;pwd=xxx';


app.get('/query/:table', function (req, res) {
    var table = req.params.table;
    //console.log('User is accessing : ' + table);
    var connection = odbc.connect(connectionString, (error, connection) => {
        connection.query("SELECT * FROM " + table + "", (error, result) => {
            if (error) { console.error(error) }
            var json = JSON.stringify(result);
            res.send(json);

    });
});

});

app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
});
app.listen(80, function () {
    console.log('Application listening on port 80');
});

在localhost / query / colli上,我的输出是:

[{“ MATRK1:” 0000019863“,” FLG1K1“:” S“},{” MATRK1“:” 0000019864“,” FLG1K1“:” S“}](以及更多行)

我的index.html看起来像这样:

<script src="https://code.jquery.com/jquery-3.4.1.slim.js"
        integrity="sha256-BTlTdQO9/fascB1drekrDVkaKd9PkwBymMlHOiG+qLI="
        crossorigin="anonymous"></script>
<script>
function getElement(id) {
  return document.getElementById(id);
}

    fetch('http://localhost/query/colli')
        .then(res => res.json())
        .then((res) => {
            const data = res;
            document.write(data)
            getElement('name').innerHTML = 'Name: ' + data;
        });
</script>
<div>
    <p id="name"></p>
</div>
<body>
</body>

但是我正在接收:

[[对象对象],[对象对象]结果

我在做什么错?我想从Node提取数据到HTML。

非常感谢您的时间:)

javascript html node.js json
3个回答
0
投票

由于data是JSON对象,因此您无法执行getElement('name').innerHTML = 'Name: ' + data;。您将必须从对象中获取一个特定的键,以得到您想要的值。


0
投票

我从console.log(data)的当前输出

(1554) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
[0 … 99]
0: {MATRK1: "0000019863", FLG1K1: "S"}
1: {MATRK1: "0000019864", FLG1K1: "S"}
2: {MATRK1: "0000019865", FLG1K1: "S"}
3: {MATRK1: "0000019866", FLG1K1: "S"}

-1
投票

取决于您的console.log,调用get方法

尝试运行此代码console.log(data.get())

© www.soinside.com 2019 - 2024. All rights reserved.