Replaced updateMsg with a proper implmentation of updating a single value within an object in a database

This commit is contained in:
Gregory Campbell
2021-11-24 23:09:58 -05:00
parent 7dfde1842f
commit 70b5a32da1
+3 -19
View File
@@ -70,27 +70,11 @@ const updateMsg = (req, res) => {
let msg = req.params.message; //get the msg message
//check if the id already exists in db
Msg.findOne({ id:id }, (err, data) => {
Msg.findOneAndUpdate({id:id}, { message:msg }, {new: true}, (err, data) => {
//if this message is not in db, add it
//if this message is in db, update it
if (data) {
//create a new msg object using the Msg model and req.params
const newMsg = new Msg({
id:id,
message:msg,
time: Date.now(),
})
Msg.deleteOne({id:id}, (err) => {
//if there's an error, return the err message
if (err) return res.json(`Something went wrong, please try again. ${err}`);
});
// save this object to database
newMsg.save((err, data)=>{
if(err) return res.json({Error: err});
return res.json(data);
})
return res.json(data);
//if there's an error or the msg is in db, return an error message
}else{
if(err) return res.json(`Something went wrong, please try again. ${err}`);