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?

4
  • What is not working? any error? Commented May 29, 2017 at 9:13
  • "console.log (wordPool);" shows "undefined" instead of array Commented May 29, 2017 at 9:41
  • @V.Pud see updated answer. Commented May 29, 2017 at 9:43
  • Possible duplicate of How do I return the response from an asynchronous call? Commented May 29, 2017 at 13:53

3 Answers 3

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
});
Sign up to request clarification or add additional context in comments.

3 Comments

What library are you using? mongoose?
yeah, it's mongoose odm
i added it to my answer.
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;

3 Comments

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?
thanks for reply, but it isn't solution. how can i operate "wordPool" in main.js? variable is still undefined
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.
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

8 Comments

is it work in sync? Because there is no callback in function. will it work for err condition?
thanks for reply, but that additional "return" returns something strange:)
@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
@V.Pud what is this "strange" something?
ok, thanks man, its tottaly new area for exploration for me, have to read about it
|

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.