0

I have a function that keeps returning an undefined object every time I call the function. I was wondering if there is a specific way of solving this problem. In index.js, I am calling the sqlQuery.getItem method and expecting it to return a row from the db however it returns undefined each time.

index.js

'use strict';
const Alexa = require('alexa-sdk');
const mysql = require('promise-mysql');
const querydb = require('./sqlQuery.js');


var testSQL = 'SELECT uWeight, uHeight from users where pin=1100';
//querydb.getItem(testSQL);

var values = querydb.getItem(testSQL);

if(values == undefined){
    console.log('Error');
}
else{
    console.log(values);
}

databaseConnection.js

var mysql = require('promise-mysql');

pool = mysql.createPool({
    host: "mytrainerdb.cbh07n2xwds2.us-east-1.rds.amazonaws.com",
    database: "trainerdb",
    user: "user",
    password: "password"
});

function getSqlConnection() {
  return pool.getConnection().disposer(function(connection) {
    pool.end(connection);
  });
}

module.exports = getSqlConnection;

sqlQuery.js

var Promise = require("bluebird");
var getSqlConnection = require('./databaseConnection');
var hello = "hello";

function getItem(sql){
    Promise.using(getSqlConnection(), function(connection) {
        return connection.query(sql).then(function(rows) {
            console.log(rows[0]);
            return (rows[0]);         
        }).catch(function(error) {
          return (error);
        });
    })
}
module.exports.getItem = getItem;
Jay
  • 53
  • 1
  • 2
  • 7
  • 3
    `getItem` isn't `return`ing anything. – 4castle Jun 05 '18 at 21:53
  • And if if it was returning something, it won't be returning what you think it should.. :) – Keith Jun 05 '18 at 21:57
  • 2
    And if it did, it would be a promise, so check: [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) – Marcos Casagrande Jun 05 '18 at 21:57
  • @4castle I know this will be a stupid question to ask but how isnt it returning anything when I asked it to return rows[0]. The line above it is displayed in the console – Jay Jun 05 '18 at 22:02
  • `rows[0]` is returned inside one of the callback functions, but not the `getItem` function. The `getItem` function only has one statement: a call to `Promise.using`. Currently the return value of `Promise.using` is being ignored. – 4castle Jun 05 '18 at 22:05
  • You need to use `return Promise.using(...);` inside `getItem`, and then use `querydb.getItem(testSQL).then(function(values) { ... });` when you call it. – 4castle Jun 05 '18 at 22:09
  • `getItem()` does not have a return statement, so it returns `undefined` by default. All of the `return` are actually inside the callbacks which are not executed until well after `getItem()` already returns. – Code-Apprentice Jun 05 '18 at 22:23

1 Answers1

2
function getItem(sql){
    return Promise.using(getSqlConnection(), function(connection) {
        return connection.query(sql).then(function(rows) {
            console.log(rows[0]);
            return (rows[0]);         
        }).catch(function(error) {
          return (error);
        });
    })
}

Notice the return in second line? this will make a difference but you still won't get the actual value var values = querydb.getItem(testSQL); in values but rather a promise which is you are returning. You will get the value in case if promises succeeds in then callback of promise object like this

var valuesPromise = querydb.getItem(testSQL);
valuesPromise.then(function(result) {
  //use result here 
})

or simply

querydb.getItem(testSQL).then(function(result) {
  //use result here 
})
Umair Abid
  • 1,453
  • 2
  • 18
  • 35