Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何将JSON数据转换为与postgres不同的格式_Node.js_Postgresql - Fatal编程技术网

Node.js 如何将JSON数据转换为与postgres不同的格式

Node.js 如何将JSON数据转换为与postgres不同的格式,node.js,postgresql,Node.js,Postgresql,首先,我是一个node.js新手,我正在努力学习如何从postgres转换JSON数据,以匹配我需要从API返回的内容。代码如下: function readUsers(req, res, next) { console.log('database string is ' + process.env.DB_CONNECTION_STRING); var client = new database.Client(process.env.DB_CONNECTION_STRING);

首先,我是一个node.js新手,我正在努力学习如何从postgres转换JSON数据,以匹配我需要从API返回的内容。代码如下:

function readUsers(req, res, next) {
    console.log('database string is ' + process.env.DB_CONNECTION_STRING);
    var client = new database.Client(process.env.DB_CONNECTION_STRING);
    client.connect();
    console.log(req.body);
    var queryString = `SELECT * from readusersbynamejson(${req.query.user_id}, '${req.query.username}', ` +
                                        `${req.query.limit_count}, ${req.query.offset})`;
    console.log("Executing: ", queryString);
    client.query(queryString, function(err, result) {
        console.log("query complete");

        client.end();

        if (err) {
            console.log("error = ", err);
            return next(err);
        } else {
            console.log("RowCount ", result.rowCount);

            var rowData = [];

            result.rows.map(function(row) {
                try {
                    console.log ("Row Data: ");
                    console.log(row);
                    console.log(row.readusersbynamejson);
                    //console.log("Trying to parse");
                    //row.data = JSON.parse(row.data);
                    console.log("Trying to set to subnode");
                    return row["readusersbynamejson"];
                    rowData[''].push(r)
                    //row.
                    //console.log("Result: " + row);
                } catch (e) {
                    console.log("Exception thrown");
                    console.log(e);
                    row.data = null;
                    return row;
                }

                //return newRow;
            });

            console.log(result.rows);

            res.status(200).json({
                status: 'success',
                //data: result.rows[0].readusersbynamejson,
                data: result.rows,
                message: 'Retrieved all data'
            });
        }
    });
}
这是postgres函数返回的结果:

{
    "status": "success",
    "data": [
        {
            "readusersbynamejson": {
                "user_id": 3,
                "auth_type": 1,
                "status": 1,
                "user_identity": "user3",
                "username": "mgarcia",
                "email_address": "mgarcia@gmail.com",
                "phone_number": "11234567890",
                "timestamp": 1537846141,
                "image_reference": "",
                "name": null,
                "bio": null,
                "friend_status": 0,
                "friend_status_initiated": 0,
                "post_count": 0
            }
        },
        {
            "readusersbynamejson": {
                "user_id": 2,
                "auth_type": 1,
                "status": 1,
                "user_identity": "user2",
                "username": "jdoe",
                "email_address": "jdoe@gmail.com",
                "phone_number": "11234567890",
                "timestamp": 1537846110,
                "image_reference": "",
                "name": null,
                "bio": null,
                "friend_status": 0,
                "friend_status_initiated": 0,
                "post_count": 0
            }
        }
    ],
    "message": "Retrieved all data"
}

我确实想从JSON中删除postgres函数名,但可能需要修改它的其他部分。有人能给我一个关于如何转换JSON的小代码片段吗?我已经为此绞尽脑汁好几个小时了,显然我在这里遗漏了一些基本的东西。

像这样的东西应该有用:

var users = result.data.map(function(x) {
  return x.readusersbynamejson;
});
在ES6中更简单:

const users = result.data.map(x => x.readusersbynamejson)

令人惊叹的。第二个代码片段对我来说非常有用。非常感谢。