3

I have problem in calling the method in my module.

There is an errorTypeError: usr.User.getAddress is not a function

I don't know how to fix this I think I have problem in my module code. I want to get the address or the result.

in my main.js

var mysql = require('mysql');
var usr = require('./user');

  var useraddress = usr.User.getAddress (id,pool); //this is how I access the method

in my user.js

exports.User = function () {

    return {
        getAddress: function (userid, pool){
            pool.getConnection(function (err, connection) {
                var options = {
                    sql: " select address from user where id = ?
                };

                var querypos = connection.query(options, [userid], function (err, results) {
                    if (err) throw err;


                });

            });
        }
   };

};
jemz
  • 4,987
  • 18
  • 62
  • 102

2 Answers2

5

You are exporting User as a factory function which returns an object with getAddress method on it. So you need to invoke (instantiate) User first:

var useraddress = usr.User().getAddress(id, pool);

Another important problem. connection.query request is asynchronous, which means that assigning getAddress result to var useraddress doesn't make sense. Instead you need to either pass callback to getAddress or use Promise pattern (check this post for great deal of details on the topic: How do I return the response from an asynchronous call?).

In your case I think something like this would be a simplest working approach:

exports.User = function () {
    return {
        getAddress: function (userid, pool){
            pool.getConnection(function (err, connection) {
                var options = {
                    sql: "select address from user where id = ?"
                };

                var querypos = connection.query(options, [userid], function (err, results, callback, errCallback) {
                    if (err) {
                      errCallback(err);
                    }
                    callback(results);
                });
            });
        }
   };
};

and usage:

usr.User().getAddress(id, pool, function(result) {
    console.log('Loaded', result);
});
Community
  • 1
  • 1
dfsq
  • 191,768
  • 25
  • 236
  • 258
  • Hi there is no error anymore, but the result of my query is not outputing – jemz Apr 26 '16 at 06:38
  • It's because `getAddress` is asynchronous. Check updated post. – dfsq Apr 26 '16 at 06:42
  • Hi, thank you last one question, how did you know that it is a factory function ? – jemz Apr 26 '16 at 06:44
  • Factory function is a function that returns new objects every time you call it. Like in your case when you call `User()` it produces object `{getAddress: function() {...}}`. There is also a whole design pattern (Factory) based on this approach. – dfsq Apr 26 '16 at 06:48
  • Thank you , your answer is very useful – jemz Apr 26 '16 at 06:51
  • No problem, glad it was useful. – dfsq Apr 26 '16 at 06:52
1

This is because usr.User does not have .getAddress property on it. To use .getAddress as a property, you need to export User as an object instead.

exports.User = {
    getAddress: function (userid, pool){
        pool.getConnection(function (err, connection) {
            var options = {
                sql: " select address from user where id = ?
            };

            var querypos = connection.query(options, [userid], function (err, results) {
                if (err) throw err;


            });

        });
      }
    };
 };

Now it does.

Amresh Venugopal
  • 9,299
  • 5
  • 38
  • 52
  • 1
    For a *useful* answer, say *what* you changed, and *why*. You've got about half of the why above, and none of the what. – T.J. Crowder Apr 26 '16 at 06:37