1

Im currently stuck at a problem within my router. I export the function as you see in the following code.

This is my Model:

"use strict";
var mongoose = require('mongoose');
var bcrypt = require("bcryptjs");
var Schema = mongoose.Schema;

var UserSchema = mongoose.Schema({
    username: {
        type: String,
        index: true,
        required: true
    },
    email: {
        type: String,
        required: true,
        unique: true
    },
    password: {
        type: String,
        required: true
    },
    firstname: {
        type: String,
        required: true
    },
    lastname: {
        type: String,
        required: true
    },
    gender: {
        type: String,
        required: true
    },
    country: {
        type: String,
        required: true
    },
    confirm: {
        type: Number
    },
    confirmcode: {
        type: String
    },
    ip: {
        type: String
    },
    signup: {
        type: Date, 
        default : Date.now
    },
    voice: {
        type: String
    },
    steam: {
        type: String
    },
    steamid: {
        type: String
    },
    steamlink: {
        type: String
    },
    battletag: {
        type: String
    },
    showsteam: {
        type: Number
    },
    showbnet: {
        type: Number
    },
    birthdate: {
        type: Date,
        required: true
    },
    language: {
        type: String
    }
});

var User = module.exports = mongoose.model('User',UserSchema);

module.exports.createUser = function(newUser, callback){
    bcrypt.genSalt(10, function(err, salt) {
        bcrypt.hash(newUser.password, salt, function(err, hash) {
            newUser.password = hash;
            newUser.save(callback);
        });
    });
}

module.exports.comparePw = function(canPw, hash, callback){
    bcrypt.compare(canPw, hash).then((res) => {
        callback(null,res);
    });
}

module.exports.findUserById = function(id,callback){
    User.findById(id,callback);
}

module.exports.getGamer  = function(count,callback){
    User.count(count,callback);
}

module.exports.findUsername = function(uname, callback){
    var query = {username:uname};
    User.findOne(query,callback);
}

module.exports.findEmail = function(email, callback){
    var query = {email:email};
    User.findOne(query,callback);
}

This is my Router:

var User = require("../../model/user");
module.exports = function(router){
    router.get("/user", function(req, res){
        var user = new User();
        user.getGamer(function(err,response){
            if(err){
                throw err;
            }else{
                res.json(response);
            }
        });
    });
}

the error i get is:

TypeError: user.getGamer is not a function

But i dont know why. Anyone can help me ?

chzn
  • 35
  • 1
  • 7

1 Answers1

0

You need to attach model methods to the schema, not to the model. Then the methods will be available on instances of your model. See this post for more.

UserSchema.methods.getGamer = function(newUser, callback) ...
Community
  • 1
  • 1
Mitch Lillie
  • 2,217
  • 18
  • 25