I have been looking for this answer for a couple days now. I have to be missing something obvious. I have a query in Node.js as shown below. I have been working with the results, but I can only view it with console.log. Any attempt to put the value in a variable or otherwise access it has not worked.
const {Client} = require('pg');
const client = new Client ({
user: 'pi',
host: 'localhost',
database: 'Farm2021',
password: 'password',
port:5432
})
function updateRadon(){
client.connect();
const query = {
text:'select radon from data where month=2 and day=8 and hour = 16 and radon>0;',
rowMode: 'array'
}
var testObject = client.query(query, (err,res) =>{
if(err){
console.error(err);
return;
}
testObject = res.row.prototype;
console.log(res);
client.end();
return testObject;
});
console.log(testObject);
};
updateRadon();
Here is a sample of the output I get:
undefined Result { command: 'SELECT', rowCount: 1, oid: null, rows: [ [ 23 ] ], fields: [
Field {
name: 'radon',
tableID: 16464,
columnID: 16,
dataTypeID: 21,
dataTypeSize: 2,
dataTypeModifier: -1,
format: 'text'
} ], _parsers: [ [Function: parseInteger] ], _types: TypeOverrides {
_types: {
getTypeParser: [Function: getTypeParser],
setTypeParser: [Function: setTypeParser],
arrayParser: [Object],
builtins: [Object]
},
text: {},
binary: {} }, RowCtor: null, rowAsArray: true, parseRow: [Function: _parseRowAsArray] }
How can I extract the useful value from this is the value of rows as a number (23) or something I can convert to a number. Any suggestions?