Mongoose save() is not a function
If you want to add instance methods to a mongoose Model you should use instance methods syntax:
var mongoose = require('mongoose');var UserSchema = new mongoose.Schema({ name: String});// Instance methodsUserSchema.methods.add = function(name, callback){ this.name = name; return this.save(callback);}module.exports = mongoose.model('User', UserSchema);
Methods can be added with methods keyword like this
var mongoose = require('mongoose');var Schema = mongoose.Schema;var userSchema = new Schema({ name: String});userSchema.methods.add = function(name,callback){ User.save({name:name}).exec(callback); // not checking logic}var User = module.exports = mongoose.model('user', userSchema);
You can do it as follows:
var mongoose = require('mongoose');var Schema = Mongoose.Schema;//schema is declared herevar userSchema = new Schema({ name: String});var user = Mongoose.model('User', UserSchema);//here we are assigning new data to user collection//this record will not be saved here//it will only check the schema is matching or not//if record is matching to schema then it will assign '_id' to that recordvar userRec = new user({"name":"Jessie Emerson"});userRec.save(function(error, userDoc) { //this is the callback function});
If you need anymore clarifications then please comment on this answer. :-)