14
標題。我想init初始化一個新的模型實例靜態方法:如何在同一模型的模式方法中創建模型實例?
var Schema = new mongoose.Schema({...});
//...
Schema.statics.createInstance = function (name, pass) {
var newPerson = new Person; // <--- or 'this', or 'Schema'?
newPerson.name = name;
newPerson.pass = pass;
newPerson.save();
return newPerson;
}
// ...
module.exports = db.model("Person", Schema);
我該怎麼做?
所以,tnx的響應。但最正確的答案將是: 1.對於使用模型的靜態方法,我們可以使用動態加載模型。 'db.model('Person')。countComments();' 2.爲了創建同一模型的新實例,我們只需要使用'var person = new this;' – Dmitry