0

I am able to get data back but I seem to be failing at getting the result back up through some methods above this:

car.js

'use strict';

var Q = require('q');
var pg = require('co-pg')(require('pg'));
var config = require('../../models/database-config');

var car = module.exports = {};

car.find = Q.async(function *(id)
{
    var query = 'SELECT id, title, description FROM cars WHERE id = ' + id;

    var connectionResults = yield pg.connectPromise(config.connection);

    var client = connectionResults[0];
    var done = connectionResults[1];

    var result = yield client.queryPromise(query);
    done();

    console.log("value: " + result.rows[0].id);

    return result.rows;
});

this returns a valid value for my console.log so I know I'm getting data back.

But now when I try to pass that back up the stack, here I seem to be losing it after this method:

database.js

module.exports = {
    models: {
        car: _carModel
    },
    find: Q.async(_find)
};

    function _find(carId)
    {
        _carModel.find(carId)
        .then(function(result){
                 console.log('result[0].id: ' + result[0].id);

                return result;
         })
        .catch(function(error){
                console.log("promise error: " + error);
         })
        .done();
    };

So this also works, I get a valid value for console.log('result[0].id: ' + result[0].id);

But now when try to call this function, I lose the result:

gateway.js

var car = database.find(carId);
                 console.log("car: " + car.id);
...

here I get a'Cannot read property 'id' of undefined]'

UPDATE #2

So I am trying to propagate now the promise up, but still get undefined for the line console.log("returned car data: " + data); 'data' is undefined.

gateway.js

module.exports = {
    data: function(someData){
        _data = someData;
    },
    find: function(text, result){

        if(!text){
            results(null);
        };

        var endpoint =  _endpoint.replace(/_text/g, text);

         _client.query(endpoint, function(results){

             var cars = [];
             var car;

             for (var i = 0; i < results.docs.length; i++){

                 var carId = results.docs[i].id;

                 car = database.find(carId)
                 .then(function(data){
                         console.log("returned car data: " + data);
                     })
                 .done();

                 cars.push(car);
             }

             result(cars);
        });
    }

database.js

'use strict';
var Q = require('q');

var _obituaryModel = require('../../models/postgreSQL/obituary');

module.exports = {
    models: {
        obituary: _carModel
    },
    find: Q.async(_find)
};

function _find(carId)
{
    _carModel.find(carId)
    .then(function(result){

            console.log('result[0].id: ' + result[0].id);

            return result;
     })
    .catch(function(error){
            console.log("promise error: " + error);
     })
    .done();
};

carModel.js

'use strict';

var Q = require('q');
var pg = require('co-pg')(require('pg'));
var config = require('../../models/database-config');

var car = module.exports = {};

car.find = Q.async(function *(id)
{
    var query = 'SELECT id, title, description FROM cars WHERE id = ' + id;

    var connectionResults = yield pg.connectPromise(config.connection);

    var client = connectionResults[0];
    var done = connectionResults[1];

    var result = yield client.queryPromise(query);
    done();

    console.log("value: " + result.rows[0].id);

    return result.rows;
});
rudolph1024
  • 962
  • 1
  • 12
  • 32
PositiveGuy
  • 17,621
  • 26
  • 79
  • 138

3 Answers3

0

You can't return data from an asynchronous function.

See here: How to return value from an asynchronous callback function?

Community
  • 1
  • 1
Wouter
  • 756
  • 3
  • 11
0

You are printing result[0].id on the console but reading result.id on the actual call:

Try

var car = database.find(carId);
             console.log("car: " + car[0].id);
...
noderman
  • 1,934
  • 1
  • 20
  • 36
0

You have a promise object at your disposal, use it.

In database.js:

return _obituaryModel.find(carId)

and gateway.js

var car = database.find(carId);
car.then(function (data) {
    console.log(data);
});
Kevin B
  • 94,570
  • 16
  • 163
  • 180