1

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?

1 Answer 1

2

The array with all the rows returned from the SELECT is res.rows: only one row was returned, so res.rows is an array of length 1. 23 is the value of the column radon (that is the only column in your SELECT).

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

Comments

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.