简体   繁体   English

从 Web API 调用数组中的格式化响应返回

[英]Formatting Response return from Web API call in array

I have a function call that returns the result of the SQL query in an array.我有一个 function 调用,它在数组中返回 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";
        });
    ;
}

the result I get is the entire large array like below.我得到的结果是整个大数组,如下所示。 I am trying to figure out a loop to extract all data individually but I realized the array can be in any size depending on the size of SQL query result.我试图找出一个循环来单独提取所有数据,但我意识到数组可以是任何大小,具体取决于 SQL 查询结果的大小。 Like below:-如下所示:-

{ "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" }] }

What is the best way to extract all these individual records so I can display them in formatted HTML?提取所有这些单独记录的最佳方法是什么,以便我可以以格式化的 HTML 显示它们?

If I understand what you're trying to achieve here, this is my solution: Assuming res is the response after you parse it, consider the following:如果我了解您要在这里实现的目标,这就是我的解决方案:假设res是您解析后的响应,请考虑以下事项:

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

This prints each row in the result in a different line.这会将结果中的每一行打印在不同的行中。

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

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