0

I was trying to fetch the data from postgres sql using nodejs.

I have been using npm pg to establish the connection to database. Using select query trying to fetch all the data from postgres but PostgreSQL returns the data in anonymous block.

I was unable to get the data from anonymous block. If try to get the data it returns as undefined.

Can anyone please help me out? Please see my output

 anonymous {

   Id: 10062,

   Animal: 'horse',

   Breed: 'American Quarter',

   Age: 'Adult',

   Sex: 'male',

   Size: 'Medium',

   Color: 'Green',

   Status: 'Homestay' 
},

From this I want to get the Breed alone, but it does not affect my functionality, bu unable to get the individual value from the block please see my code below:

function selectAllRecords{
    /**
    * pool is my connection object,initiall a pool of connection is created
    * This function fetch all the records from the table
    */
    pool.connect(function(err, client, done) {               
        if(err) {
            return console.error('error fetching client from pool', err);               
        }    
        client.query('select * from petpoint', function(err, result) {            
            done(); 
            if(err) {
                return console.error('error running tracking table query', err);}
                console.log(result.rows);               //output will be in postgres anonymous block
                console.log(result.rows.Id)             //undefined
                console.log(result.rows.anonymous.Id)   //undefined
                });
            });
            pool.on('error', function (err, client) {  
                    console.error('idle client error', err.message, err.stack)
            })
    }
2
  • Possible duplicate of Node.js 6 and anonymous objects Commented Feb 20, 2017 at 17:24
  • 2
    I was unable to get the data from anonymous block - that's a bit of a nonsense. When an object is instantiated anonymously, it doesn't affect the object's functionality. If you were to show the code, the mistake would be easy to spot. Commented Feb 20, 2017 at 17:28

0

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.