版本:
mongoose:4.4.19
nodeJS:4.4.3
student类代码:
var mongoose=require("mongoose");
var db=require("./db.js");
var studentSchema=new mongoose.Schema({
name : {type:String},
age :{type:Number},
sex :{type:String}
});
studentSchema.statics.find=function (name,callback) {
this.model("student").find({"name":name},callback);
}
studentSchema.static.update=function (conditions,update,options,callback) {
this.model("student").update(conditions,update,options,callback);
}
var studentModel=db.model("student",studentSchema);
module.exports=studentModel;
Test代码
var student=require("./models/student");
student.find("小hong",function (err,result) {
console.log(result);
})
运行Test文件,报错:
RangeError: Maximum call stack size exceeded
原因:
查看了Stack Overflow 上的一些相关错误文章,大部分是关于插入较大的数据造成的,而我这里不存在这种情况。觉得可能是自己写的类方法名称有问题(与包里面的api方法可能存在名称冲突),就把find和update改为findOne和updateOne。
修改后student类代码:
var mongoose=require("mongoose");
var db=require("./db.js");
var studentSchema=new mongoose.Schema({
name : {type:String},
age :{type:Number},
sex :{type:String}
});
studentSchema.statics.findOne=function (name,callback) {
this.model("student").find({"name":name},callback);
}
studentSchema.static.updateOne=function (conditions,update,options,callback) {
this.model("student").update(conditions,update,options,callback);
}
var studentModel=db.model("student",studentSchema);
module.exports=studentModel;
标注修改图: