0

First of all, I'm a node.js newbie and I'm struggling with how to transform JSON data from postgres to match what I need to return from my API. Here is the code:

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'
            });
        }
    });
}

This is what the postgres function returns:

{
    "status": "success",
    "data": [
        {
            "readusersbynamejson": {
                "user_id": 3,
                "auth_type": 1,
                "status": 1,
                "user_identity": "user3",
                "username": "mgarcia",
                "email_address": "[email protected]",
                "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": "[email protected]",
                "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"
}

I really want to remove the postgres function name from JSON but I might need to modify other portions of it. Can someone give me a small code snippet on how to transform JSON? I've wrestled with this for several hours and clearly I'm missing something fundamental here.

1 Answer 1

1

Something like this should work:

var users = result.data.map(function(x) {
  return x.readusersbynamejson;
});

Even simpler in ES6:

const users = result.data.map(x => x.readusersbynamejson)
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome. The second code snippet work great for me. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.