0

I have created number of function in the user model which are both instance and class methods. But when i am calling class method findMyMobile() from a controller it is giving 'not a function' error. I tried display it inside the controller but it seems it is undefined there.

model/user.js

const { Sequelize, sequelize } = require('../db/sequelize');
const jwt = require('jsonwebtoken');

const Model = Sequelize.Model;

class User extends Model {}
User.init({
    id:{
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    name: {
        type: Sequelize.STRING,
        allowNull: false
    },
    mobile_number:{
        field:'mobile_number',
        type: Sequelize.BIGINT(10),
        unique:true,
        allowNull: false,
        is:/^[1-9]\d{9}$/g,
    },
    type:{
        type: Sequelize.ENUM('0','1','2'),
        allowNull: false,
        defaultValue: '1',

    },
    otp:{
        type: Sequelize.STRING,

    },
    createdAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW

    },
    updatedAt: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW
    }
},{ sequelize,
    modelName:'user',
    classMethods:{

        findByMobile: function(){
            var User = this;

            return User.findOne({'mobile_number':data['mobile_number']}).then(user => {

                return new Promise((resolve,reject)=>{
                    if(user)
                        resolve(user);
                    else
                        reject(new Error('No user found'));
                });

            }).catch(err=>{
                return Promise.reject(new Error('Database error'));
            })
        }
}
})

User.sync();

module.exports = {
    User
}

controller/login.js

const { User } = require('../model/user');
const _ = require('lodash');



exports.login = (req, res) => {
    const mobile = _.pick(req.body, ['mobile_number']);
    console.log(typeof User.findByMobile);
    User.findByMobile(mobile).then((user) => {
       console.log(user);

    }).catch(err => {
        var response = {
            status: 'failure',
            message: err.message
        }
        res.send(response);
    });

};

ERROR:

TypeError: User.findByMobile is not a function
Ujjwal Mishra
  • 177
  • 1
  • 8

2 Answers2

0

I think you have the export wrong. See this example for reference.

module.exports = {
  getName: () => {
    return 'Jim';
  },

  getLocation: () => {
    return 'Munich';
  },

  dob: '12.01.1982',
};

Then on the import file:

const { getName, dob } = require('./user');
console.log(
  `${getName()} was born on ${dob}.`
);

What I do suggest is export the function itself. See link below for ref: What is the purpose of Node.js module.exports and how do you use it?

Aleksandar Zoric
  • 1,343
  • 3
  • 18
  • 45
  • i don't think there is some problem with exports.login() because i've have used it in same manner with mongoose and worked as expected. If there is anything else that you can think of please tell and thanks for ur answer. – Ujjwal Mishra May 15 '20 at 16:26
0

Since sequelize v4 classMethods and instanceMethod are removed from the configuration options : https://sequelize.org/v4/manual/tutorial/upgrade-to-v4.html#config-options

You have two ways to define them

class User extends Model {
    //Class Method
    static findByMobile() {}
    //Instance Method
    findByMobile() {}
}

OR

class User extends Model { }
//Class Method
User.findByMobile = function() {}
//Instance Method
User.prototype.findByMobile = function() {}
mousto090
  • 1,939
  • 1
  • 13
  • 12