Im trying to add new data to database ,only if it isn't already present.I ask user to enter name ,and if the name already exists he/she can't enter corresponding data,and an error is to be shown saying it "already exists". This is what im doing-
app.post("/new",function(req,res){
Model.findOne({"name":req.body.name},function(err,persons){
if(err!=null)
{ req.flash("error","Already Exists");
res.redirect("/new");}
if(err==null)
{ Model.create(req.body.person,function(err,newPerson){
if(err){console.log("Error");}
else{
newPerson.name=req.user.name;
newPerson.id=req.user._id;
newPerson.save();
res.redirect("/");}
});}
});
});
But when i use this,even though i enter already present data,it still adds it to the database. Im using express,node js and mongodb
via Ravi Hooda
No comments:
Post a Comment