Created
February 1, 2016 02:51
-
-
Save richzw/46215594763863ba5520 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var peopleSchema = new mongoose.Schema({ | |
name: { | |
type: String, | |
required: true, | |
minlength: 3, | |
maxlength: 25 | |
}, | |
age: Number | |
}); | |
peopleSchema.statics.testValidate = function(person) { | |
return new Promise((res, rej) => { | |
const pObj = new this(person); | |
pObj.validate(err => { | |
if (err) { | |
return rej(err); | |
} else | |
return res('Success'); | |
}); | |
}); | |
} | |
var People = mongoose.model('People', peopleSchema); | |
function insertData() { | |
var p = new People({ | |
name: '', | |
age: 23 | |
}); | |
People.testValidate(p) | |
.then(data => { | |
console.log('OK', data); | |
}) | |
.catch( err => { | |
console.error('FAILED:',err) | |
}) | |
.finally(() => mongoose.connection.close()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment