This is my schema
var mongoose = require('mongoose');
var telecallingSchema = new mongoose.Schema(
{
"empid": {type: String, required: true},
"name": {type: String, required: true},
"firmname": {type: String},
"product": {type: String, required: true},
"dproduct": {type: String, required: true},
"state": {type: String, required: true},
"district": {type: String, required: true},
"city": {type: String, required: true},
"contact": {type: Number, required: true, unique: true},
"refnums": [],
"wrongnum": {type: Boolean, default: false},
"service": {type: String, default: 'TeleCalling'},
"followups": [
{
"followup": {type: Date, default: Date.now},
"actiondone": {type: String, required: true},
"assignedto": {type: String, required: true},
"result": {type: String, required: true},
"nextaction": {type: String},
"nextfollowup": {type: Date},
"reason": {type: String, required: true},
"remarks": {type: String},
"createdAt": {type: Date, default: Date.now},
"modifiedAt": {type: Date, default: Date.now},
"isDeleted": {type: Boolean, default: false}
}
],
"createdAt": {type: Date, default: Date.now},
"modifiedAt": {type: Date, default: Date.now},
"isDeleted": {type: Boolean, default: false}
}
);
mongoose.model('telecalling', telecallingSchema);
In the above schema the fields which are required in the followups array are not showing validation error if they are not given in request.body while adding and it is saved in the database.
Here is the controller code for adding
newTelecalling: function(request,response){
var telecallings = new telecalling(request.body);
telecallings.save(function(error,result){
if(error){
response.send(error);
console.log(error);
}
else{
telecalling.findOneAndUpdate({_id: result._id}, {$push: {followups: request.body}}, {safe: true, new: true}, function(err,res){
if(err){
response.json(err);
}
else{
response.json(res);
}
});
}
});
}
And one more thing is, while updating the above saved data it is showing validation error in a surprising way. For Ex., if I have not added the reason field in followups array, while adding it is getting saved and when trying to add it while updating it is showing validation error.
Here is the controller code for updating
updateTelecalling:function(request,response){
telecalling.findById({"_id":request.body._id},function(error,result){
if(error){
response.send(error);
}
else{
for(prop in request.body){
result[prop] = request.body[prop]
}
result.save(function(err,res){
if(err){
response.send(err);
console.log(err);
}
else{
console.log(res);
telecalling.findOneAndUpdate({"_id": res._id, "followups": {$elemMatch: {"_id": request.body.fupid}}}, {$set: {"followups.$.actiondone" : request.body.actiondone, "followups.$.assignedto" : request.body.assignedto, "followups.$.result" : request.body.result, "followups.$.nextaction" : request.body.nextaction, "followups.$.nextfollowup" : new Date(request.body.nextfollowup), "followups.$.reason" : request.body.reason, "followups.$.remarks" : request.body.remarks}}, {safe: true, new: true}, function(e,r){
if(e){
console.log(e);
response.send(e);
}
else{
response.json(request.body);
}
});
}
});
}
});
}
How to overcome this?
via G.Mounika
No comments:
Post a Comment