Reputation:
the Node js code written below should give some error and stop but it compile properly and give unexpected product.
the code below doest not throwing error instead it runs and give output of two line like below:-
santu
connnection estabished
and then it stops
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost:27017/mynewdb').
then(()=>{console.log("connnection estabished")})
.catch((err)=>{console.log(err)});
//create a schema
const createschema = new mongoose.Schema({
name : {
type : String
},
email:{
type : String,
validate(value){
console.log(value);
if(validator.isEmail(value)===false){
throw new Error('invalid Email');
}
}
}
})
//creating a collection/model
const mycollection1 = new mongoose.model("mycollection1",createschema);
const insertdata = async()=>{
try{
const doc1 = new mycollection1({
name : "santu1",
email : "santu"
})
const mydata = await doc1.save();
console.log(mydata);
}
catch{(err)=>{
console.log(err);
}}
}
insertdata();
Upvotes: 0
Views: 32
Reputation: 363
The validate()
function for the email
field in the schema definition needs to return a boolean, but you're throwing an error instead. Try this:
validate(value){
return validator.isEmail(value)
}
Upvotes: 1