I'm facing an issue while working on a REST API using JS. The code is functioning fine, but I encounter a problem when trying to delete, put, or patch an entry that doesn't exist. Instead of returning an error, it displays a success message. The GET route, however, works perfectly.
app.route("/verbrauch/:parameterVariable")
.get((req, res) => {
Verbrauch.findOne({
bezeichnung: req.params.parameterVariable
}, (err, foundEntries) => {
if (foundEntries) {
res.send(foundEntries);
} else {
res.send("Success");
}
});
})
.put((req, res) => {
Verbrauch.update({
bezeichnung: req.params.parameterVariable
}, {
bezeichnung: req.body.bezeichnung,
stueckzahl: req.body.stueckzahl,
monat: req.body.monat,
jahr: req.body.jahr,
}, {
overwrite: true
},
err => {
if (!err) {
res.send("Success");
} else {
res.send(err);
}
}
);
})
.patch((req, res) => {
Verbrauch.update({
bezeichnung: req.params.parameterVariable
}, {
$set: req.body
},
err => {
if (!err) {
res.send("Success")
} else {
res.send(err);
}
}
);
})
.delete((req, res) => {
Verbrauch.deleteOne({
bezeichnung: req.params.parameterVariable
},
err => {
if (!err) {
res.send("Success");
} else {
res.send(err);
}
}
);
});