【问题标题】:Formatting Response return from Web API call in array格式化来自数组中 Web API 调用的响应返回
【发布时间】:2020-07-20 09:22:49
【问题描述】:

我有一个函数调用,它以数组的形式返回 SQL 查询的结果。

app.get('/alluser', (req, res) => {
    console.log("All Users")
    user.getAllUsers(function (err, result) {
        if (!err) {
            console.log(result)
            res.send({ result })

        } else {
            res.send(err.errno + " No record");
        }
        res.end()
    });
});
function myFunction() {
    console.log("Going");
    axios.get('http://localhost:8081/alluser')
        .then(function (response) {
            console.log(response.data);
            var msgtxt = JSON.stringify(response.data);
            console.log(JSON.parse(msgtxt));
            //var msgtxt = JSON.parse(msgtxt);
            document.getElementById("demo").innerHTML = msgtxt;
        })
        .catch(function (error) {
            document.getElementById("demo").innerHTML = "Bad response";
        });
    ;
}

我得到的结果是整个大数组,如下所示。我试图找出一个循环来单独提取所有数据,但我意识到数组可以是任何大小,具体取决于 SQL 查询结果的大小。如下:-

{ "result": [{ "userid": 3, "username": "Susan Teo", "email": "susan@hotmail.com", "role": "user", "password": "12345678" }, { "userid": 6, "username": "Susanto Morlon", "email": "susanto@melon.com", "role": "user", "password": "12345" }, { "userid": 7, "username": "Jim Wash", "email": "Jimmy@hotmail.com", "role": "user", "password": "123454" }, { "userid": 8, "username": "Vatsun Khun", "email": "vatsun@hotmail.com", "role": "admin", "password": "password" }, { "userid": 9, "username": "Susan Tan", "email": "susantan@hotmail.com", "role": "admin", "password": "123454" }, { "userid": 21, "username": "Ronnie Bowey", "email": "ronnieb@hotmail.com", "role": "admin", "password": "123454" }, { "userid": 23, "username": "Margaret Tan", "email": "magtan@hotmail.com", "role": "admin", "password": "123454" }, { "userid": 25, "username": "Jane Teo", "email": "janeteo@hotmail.com", "role": "admin", "password": "12345" }, { "userid": 27, "username": "Martin Luther", "email": "mluther@good.com", "role": "user", "password": "123456" }, { "userid": 29, "username": "James Hwang", "email": "james@look.com", "role": "admin", "password": "uyghj" }, { "userid": 30, "username": "Donald Trump", "email": "donald@usa.com", "role": "user", "password": "12345" }, { "userid": 31, "username": "Maki", "email": "maki@gmail.com", "role": "user", "password": "12344" }, { "userid": 32, "username": "MakiXXX", "email": "makiato@gmail.com", "role": "user", "password": "12345" }, { "userid": 33, "username": "James Toto", "email": "jamest@hotmail.com", "role": "admin", "password": "12345" }, { "userid": 34, "username": "Ronald Tan", "email": "r99supe@gmail.com", "role": "admin", "password": "12345" }] }

提取所有这些单独记录以便我可以在格式化的 HTML 中显示它们的最佳方法是什么?

【问题讨论】:

  • 您希望如何将数据显示为简单的文本?每一行都是一个新元素,还是不同的格式?

标签: javascript sql node.js json


【解决方案1】:

如果我了解您在这里想要实现的目标,这就是我的解决方案: 假设 res 是解析后的响应,请考虑以下内容:

Object.entries(res.result).map(entry => {
        console.log( entry[1]);
    });

这会将结果中的每一行打印在不同的行中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多