Friday, 26 May 2017

Javascript function always returns undefined [duplicate]

This question already has an answer here:

I'm writing a program in NodeJS.

The program is separated into two files. the main file

hello.js
dbc.js

dbc is a module export, to keep the code clean. A function in dbc always returns undefined.

Here's my code.

Hello.js

// Establish connection with cache and database
const mysql = require('mysql2');
const Memcached = require('memcached');
const memcached = new Memcached('localhost:11211');
const bb = require('bot-brother');

//Load the database cache functions
const dbcLib = require("./dbc");
const dbc = dbcLib(memcached);

// create the connection to database
const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  database: 'test'
});


const bot = bb({
    key : 'xxxx',
    polling: { interval: 0, timeout: 1 }
});


bot.command('start').invoke(function (ctx){
    var x = dbc.check_cache('Haider');
    console.log("return function got "+typeof x);
});

dbc.js
module.exports = function (memcached) {
  return {
    check_db : function(key){},

    check_cache : function(key){
      memcached.get(key,function(err, data){
          console.log(data);
          console.log("Type is "+ typeof data)
        if(typeof data !== "undefined"){
            return data;
        } else {
            return false;
        }
      });
    }
}
};

I am not sure why its returning undefined, some help would be appreciated



via Marshall Mathews

No comments:

Post a Comment