0

I'm working with the "architect" plugin system package. I've created two plugins one's named "database" and one "test".

database provides database and test consumes database.

following issue happens with this implementation:

when calling my method getClientOnlineTimeById from the test plugin the method will process and exec the callback but when using the passed data in my callback function at for example setting a variable it is undefined everytime. tried promise as well nothing changed still undefined.

snip from database.js:

    getClientOnlineTimeById: function(clientDatabaseId, callback) {

        // Get a mysql connection from our created pool
        pool.getConnection(function(err, conn){
            conn.release();

            if(err) {
                throw err;
            }

                // exec our query to get our client with our clientId
            conn.query("SELECT `time` FROM `onlinetime` WHERE `clientDatabaseId` = ?", [clientDatabaseId], function(err, rows){
                if(err){
                    // call callback and let it handle the error
                    callback(err);
                }

                if(options.debug)
                    console.log("[Database:getClientOnlineTimeById] returning row with data: " + JSON.stringify(rows));

                // call callback with data
                callback(null, rows);
            });
        });

    }

snip from test.js:

var onlineTime = undefined;
db.getClientOnlineTimeById("5", function(err, data) {
    if(err) {
        throw err;
    }

    onlineTime = data;
});

console.log("onlineTime: \n" + JSON.stringify(onlineTime)); 

console log:

onlineTime: undefined
[Database:getClientOnlineTimeById] returning row with data:[{"time":34501218327}] <-- This is the console.log from the getClientOnlineTimeById function

when i add a setTimeout infront of my console.log in the test.js it works, but i need the data as soon as i'm in the callback function. Is there anything i'm missing?

Slowline
  • 1
  • 1
  • 1
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Blue Jan 25 '18 at 19:35
  • Your console.log statement will execute prior to the execution of your callback method. And yes, there are some other posts where this kind of question has already has been answered. – Ravi Jan 25 '18 at 20:03
  • *tried promise as well*...I don't see that code anywhere? – Blue Jan 25 '18 at 20:13

0 Answers0