1

getting undefined all the time "main.js":

var dbAccess = require('../dao/dbAccess');
dbaInstance = new dbAccess();
var wordPool = dbaInstance.getWordPool();
console.log (wordPool);

and "dbAccess.js" contains:

var DatabaseAccess = function() {}

DatabaseAccess.prototype.getWordPool = function () {
    RoundWord.find({},'words decoys', function(err, wordPoolFromDB) {
        if (err) throw err;
        //console.log(wordPoolFromDB); -working ok
        return (wordPoolFromDB);
    });
}

module.exports = DatabaseAccess;

why is it not working?

V.Pud
  • 53
  • 2
  • 8

3 Answers3

2

DatabaseAccess.prototype.getWordPool is not returning any result.

Since you are using an asynchronous function, you need do one of these things:

a) Take a callback as parameter and invoke the callback with a result

DatabaseAccess.prototype.getWordPool = function (cb) {
    RoundWord.find({}, 'words decoys', function(err, results) {
        if (err) {
           return cb(err, null);
        }

        cb(null, results);
    });
}

The callback convention is: cb(error, results...)

b) Use promises

DatabaseAccess.prototype.getWordPool = function () {
    return RoundWord.find({}, 'words decoys', function (err, results) {
        if (err) {
            throw err; // however you might want to sanitize it
        }

        return results;
    });
}

To consume this result you will need to do it as a promise

databaseAccess.getWordPool()
.catch(function (err) {
    // process the error here
})
.then(function (results) {
    // something with results
});
arboreal84
  • 2,086
  • 18
  • 21
1

If the function is Asynchronous you need to pass a callback to find to get the result:

DatabaseAccess.prototype.getWordPool = function (callback) {
    RoundWord.find({},'words decoys', function(err, wordPoolFromDB) {
        if (err) throw err;
        callback(err, wordPoolFromDB);
    });
}

and call it as follows in main:

dbaInstance.getWordPool(function (err, wordPool) {
    console.log (wordPool); 
   // wordPool is only available inside this scope,
   //unless assigned to another external variable
});

// cannot access wordPool here
gotomanners
  • 7,808
  • 1
  • 24
  • 39
  • is it work in sync? Because there is no callback in function. will it work for err condition? – Prashant Tapase May 29 '17 at 09:18
  • thanks for reply, but that additional "return" returns something strange:) – V.Pud May 29 '17 at 09:20
  • @PrashantTapase Well i'd need to know what `Roundwood.find` does or returns. I assumed it synchronous because of how the code was written. like `Array.prototype.find` – gotomanners May 29 '17 at 09:24
  • @V.Pud what is this "strange" something? – gotomanners May 29 '17 at 09:24
  • @PrashantTapase In err condition, the func will throw err. Its up to OP to safeguard in a try catch and handle as appropraite. – gotomanners May 29 '17 at 09:30
  • ok, thanks but how can i operate variable "wordPool" in main "dbaInstance.getWordPool(function () { console.log (wordPool); });" – V.Pud May 29 '17 at 09:49
  • @V.Pud you can only use the `wordPool` variable inside the context of the `getWordPool` callback. This is how async programming works. If you post more code, I'll be able to show you next steps. – gotomanners May 29 '17 at 09:52
  • 1
    ok, thanks man, its tottaly new area for exploration for me, have to read about it – V.Pud May 29 '17 at 13:56
1

It will work if you change to this:

var dbAccess = require('../dao/dbAccess');
dbaInstance = new dbAccess();
dbaInstance.getWordPool(function(wordPool){console.log (wordPool);});

And:

var DatabaseAccess = function() {}

DatabaseAccess.prototype.getWordPool = function (cb) {
    RoundWord.find({},'words decoys', function(err, wordPoolFromDB) {
        if (err) throw err;
        //console.log(wordPoolFromDB); -working ok
        cb(wordPoolFromDB);
    });
}

module.exports = DatabaseAccess;
srfrnk
  • 2,459
  • 1
  • 17
  • 32
  • can we write `var DatabaseAccess = function (cb) { RoundWord.find({},'words decoys', function(err, wordPoolFromDB) {` with above code? What is roll of prototype in above code? – Prashant Tapase May 29 '17 at 09:28
  • thanks for reply, but it isn't solution. how can i operate "wordPool" in main.js? variable is still undefined – V.Pud May 29 '17 at 09:29
  • If you use asynchronous functions you have to consume the results in an asynchronous fashion. You can't mix synchronous and asynchronous code like that. You can operate on workPool inside the callback or use a blocking synchronous function to get the results but not both. sorry. – srfrnk May 29 '17 at 09:58