anon
anon

Reputation:

Node.js Mongoose static function not getting called

Recently i've been trying to rewrite my node.js express app to be more in line with the mvc principle. I've also been trying to incorporate mongoose. I'm having a problem with calling the static functions on a mongoose model.

userSchema.statics.findDuplicates = function (cb) {
    console.log("Duplicates called");

    this.findOne({ email: this.email }, function(err, result){
        if (err) throw err;
        if (result) {
            cb("A user with this email has already been created.");
        } else {
            cb("");
        }
    });
}

Now the problem is that i'm later exporting a model using this schema, so this is all contained in one file:

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

When i later call this inside a controller, (obviously requiring and initiating the model beforehand):

user.findDuplicates(function(result){

    if (result) {
        res.send("Selle e-mailiga kasutaja on juba loodud.");
        console.log("Duplicates");
    } else {
        user.save();
        res.send("Kasutaja loodud.");
        console.log("User created with password.")
    }
});

It just never gets called. Node tells me it accepted a post, but got a 500 internal server error, and the "Duplicates called" inside findDuplicates does not appear in the console. Something is very wrong here, and i do not know how to fix it.

EDIT: Full controller code:

var express = require('express');
var router = express.Router();

var User = require("../models/user.js");

router.get('/', function(req, res, next) {
    res.render('users',{title: "Lisa kasutaja"});
});

router.post('/', function(req, res, next) {
    var query = req.body;
    var message = "";

    console.log("Post recieved " + JSON.stringify(query));

    if (query.password != query.repeatPassword){
        res.send("Paroolid ei ole võrdsed.");
        console.log("Passwords don't match");
    } else {
        var user = new User({
            firstName: query.firstName,
            lastName: query.lastName,
            telephone: query.telephone,
            email: query.email,
            password: query.password
        });

        console.log("User created");

        user.findDuplicates(function(result){

            if (result) {
                res.send("Selle e-mailiga kasutaja on juba loodud.");
                console.log("Duplicates");
             } else {
                user.save();
                res.send("Kasutaja loodud.");
                console.log("User created with password.")
            }
        });
    }

});

module.exports = router;

Upvotes: 1

Views: 5623

Answers (1)

Rodrigo Medeiros
Rodrigo Medeiros

Reputation: 7862

Your problem resides in the fact that you're calling a static method in an instance of a model, which is not correct. See the difference below:

// if you define a static method
userSchema.statics.findDuplicates = function (cb) {
  // do your stuff
}

// you call it this way
var User = require("../models/user.js");
User.findDuplicates(function (result) {
  // do your stuff
});

// if you define an instance method
userSchema.methods.findDuplicates = function (cb) {
  // do your stuff
};

// you call it this way (on an instance of your model)
var User = require("../models/user.js");
var user = new User({
      firstName: query.firstName,
      lastName: query.lastName,
      telephone: query.telephone,
      email: query.email,
      password: query.password
    });
user.findDuplicates(function (result) {
  // do your stuff
});

Upvotes: 8

Related Questions