V
V
vetsmen2017-01-30 02:17:48
JavaScript
vetsmen, 2017-01-30 02:17:48

Again this Promis?

I have this model:

connection = require('../lib/mysql');
var info = {};
var getBalance = function(id) {
  return new Promise(function(resolve, reject){
      connection.query('SELECT balance FROM Users WHERE id = ?', id, function(error, result, fields) {
      	if(error)
      		reject(error);
          if(result[0]['balance'])
              resolve(result[0]['balance']);
          reject();
      });
  });
};
info.getBalance = function(id) {
  getBalance(id).then(function(data){
    console.log(data);
    return data;
  });
}
module.exports = info;

In another file I do:
var model = require('./model');
console.log(model.getBalance(123123));

And he gives me underfind, although console.log (data) displays the desired result in that file. How to avoid it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AnjeyTsibylskij, 2017-01-30
@vetsmen

You forgot to return a promise

info.getBalance = function(id) {
  return getBalance(id).then(function(data){
    console.log(data);
    return data;
  });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question