I am using express to add/update/delete the data on MongoDB. Good think is its working fine.
Updated
function UpData(chk, _action, up){
Employee.updateOne( chk ,{ _action : up})
.then(data => {
if (!data) {
res.status(404).send({
message: `Cannot update Employee with id=${id}. Maybe Employee was not found!`
});
} else res.send({ message: "Absence is added successfully." });
})
.catch(err => {
res.status(500).send({
message: "Error updating Employee with id=" id
});
});
}
This isn't working, ca anybody help if I doing any mistake with syntax?
I guess the issue action, when I tried add console.log in function before update:
console.log( chk ,{ _action : up})
console.log(_action)
Output:
{ _id: '691fa64', 'absences.ab_id': 'MEk' } { _action: { 'absences.$.ab_comments': { comment: 'K1' } } }
$push
The question why in both console.log action value $push didnt print?
CodePudding user response:
New
const updateEmployee = (id, body, chk, cb) => {
Employee.updateOne(
{ '_id': id, 'absences.ab_id': body.id }, chk
).then(data => {
if (!data) {
return cb(`Cannot update Employee with id=${id}. Maybe Employee was not found!`);
} else {
return cb(null, "Absence is added successfully.");
};
})
.catch(err => {
return cb(`Error updating Employee with id=${id}`)
});
}
exports.update = (req, res) => {
if (!req.body) {
return res.status(400).send({
message: "Data to update can not be empty!"
});
}
const id = req.params.id;
if (req.body.iscom) {
if (req.body.isadd) {
console.log(id, req.body);
updateEmployee(id, req.body, { '$push': { 'absences.$.ab_comments': req.body.arr } }, (err, message) => {
if (err) {
res.status(404).send({
message: err
});
}
res.send({message});
});
} else {
console.log(id, req.body);
updateEmployee(id, req.body, { '$push': { 'absences.$.ab_comments': req.body.arr } }, (err, message) => {
if (err) {
res.status(404).send({
message: err
});
}
res.send({message});
});
}
}
}
Old
exports.update = (req, res) => {
if (!req.body) {
return res.status(400).send({
message: "Data to update can not be empty!"
});
}
const id = req.params.id;
if (req.body.iscom) {
if (req.body.isadd) {
console.log(id, req.body)
Employee.updateOne({ '_id': id, 'absences.ab_id': req.body.id }, { '$push': { 'absences.$.ab_comments': req.body.arr } })
.then(data => {
if (!data) {
res.status(404).send({
message: `Cannot update Employee with id=${id}. Maybe Employee was not found!`
});
} else res.send({ message: "Absence is added successfully." });
})
.catch(err => {
res.status(500).send({
message: "Error updating Employee with id=" id
});
});
}
else {
console.log(id, req.body)
Employee.updateOne({ '_id': id, 'absences.ab_id': req.body.id }, { '$pull': { 'absences.$.ab_comments': req.body.arr } })
.then(data => {
if (!data) {
res.status(404).send({
message: `Cannot update Employee with id=${id}. Maybe Employee was not found!`
});
} else res.send({ message: "Absence is added successfully." });
})
.catch(err => {
res.status(500).send({
message: "Error updating Employee with id=" id
});
});
}
}
}
CodePudding user response:
Try to give []
to the varible which not print value, as below
console.log( chk ,{ [_action] : up})