Friday, 17 March 2017

can't get data from function in node.js

I want to send data next to console.log function,but return of posts() appears to be undefined.

I am trying to figure out how to make this code work properly:

var posts = function ()
{
    var pg = require('pg');
    var connect = "postgres://egor:123456@127.0.0.1/blog-site";
    pg.connect(connect, function (err, client, done) {
        if (err) {
            return console.error('error fetching client from pool', err);
        }
        client.query('SELECT * FROM blog', function (err, result) {
            if (err) {
                return console.error('error running query', err);
            }
            //console.log(result.rows);
            return result.rows;
            done();
        });
    });
}

posts().then(posts => console.log(posts));



via Егор Зотов

No comments:

Post a Comment