0

I'm trying to SELECT data from a Postgresql database, using Node.js. I have been able to return the result, but I'm having trouble accessing the contents of the result. My query code looks like this:

select: function(selection, table, condition, limit,callback)
{
    pool.connect(function(err, client, done)
    {
        let sql;
        let rows = [];
        if(selection === 'ALL')
        {
            sql = 'SELECT * FROM ' + table + ' WHERE ' + condition;
        }
        else
        {
            sql = 'SELECT ' + selection + ' FROM ' + table + ' WHERE ' + condition;
        }

        if(isDefined(limit))
        {
            sql = sql + ' LIMIT ' + limit;
        }
        if (err)
        {
            return console.error('Error acquiring client', err.stack);
        }
        if (sql === '')
        {
            console.log("Error: No SQL statement!");
        }
        else
        {
            console.log("Running query \'" + sql + "\'.");
            client.query(sql,
                function(err, result)
                {
                    if (err)
                    {
                        console.log('Query error: ' + err);
                    }
                    else
                    {
                        rows = result.rows;
                        callback(rows);
                    }
                });
        }
    });
},

The callback passes "rows". The code where this is picked up looks like this:

    let selection = 'prefname';
    let table = 'users';
    let condition = `fbid='${senderID}'`;
    let limit = 1;
    let dbData = [];
    let rowString;
    let greetingText;
    userData.FBUser = uName;
    db.select(selection,table,condition,limit, function(res)
        {
            dbData = res;
            if (!isDefined(dbData))
            {
                console.log("Nothing returned!");
            }
            else
            {
                rowString = JSON.stringify(dbData);
                console.log("Returned: " + rowString);
                for (let key in rowString)
                {
                    if (rowString.hasOwnProperty("prefname"))
                    {
                        console.log("TRUE");
                    }
                }
            }
        }
    );

Now, when I run this code, I can see that SOMETHING has been returned from my database. In my console log, I see this:

2018-01-25T17:57:09.465642+00:00 app[web.1]: Running query 'SELECT prefname FROM users WHERE fbid='1769596366406807' LIMIT 1'.
2018-01-25T17:57:09.470727+00:00 app[web.1]: Returned: [{"prefname":"Sam"}]

So I know SOMETHING has been returned, but for some reason I cannot get into the JSON string to pull it out. I've tried using for...in, but it hasn't worked.

Is there a better way to do what I'm trying to do? Am I even doing it right at all?

1 Answer 1

1

The data is coming back via the variable res. You then immediately assign a reference to that same data under the name dbData. You take dbData, and stringify it into a new variable rowString. Now you use rowString, but it's simply a string, not an iterable object. You need to operate against the dbData object to iterate over it's properties, and not rowString.

dbData looks to be an array of objects, so you'll need to ensure you iterate over it as such. Could use something like:

for(let i = 0; i < dbData.length; i +=1{
  let obj = dbData[i];
  for(let key in obj){
    console.log(`Key: ${key}`);
    console.log(`Value: ${obj[key]}`);
  }
}

Untested so just shooting from the hip here, but it'd be something like that...

Sign up to request clarification or add additional context in comments.

1 Comment

OK, well that works. I assigned "res" (the data from the query) to "dbData" and then when I tried your little iteration, it worked, and outputted the key details to the console. The reason I was doing the long convoluted thing was because I couldn't figure out how to access the object. Thanks for the help!

Your Answer

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