Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method name is not a function

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;


                });

            });
        }
   };

};
like image 937
jemz Avatar asked Jun 30 '26 11:06

jemz


2 Answers

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);
});
like image 108
dfsq Avatar answered Jul 02 '26 01:07

dfsq


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.

like image 38
Amresh Venugopal Avatar answered Jul 02 '26 00:07

Amresh Venugopal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!