——————————– SCRIPT 1 ————————
`
addCereal: (req, res) => {
cerealDB.find({ name: req.body.name }).then(result => {
if (!result.length) {
const cerealInfo = new cerealDB({
name: req.body.name,
company: req.body.company,
year: req.body.year,
})
cerealInfo.save().then(result => {
res.status(200).json({ message: 'Data inserted successfully!', result })
})
————-`———————————————————–
——————————— SCRIPT 2 —————————–
`deleteCereal: (req, res) => {
cerealDB.find({ name: req.params.searchQuery }).then(result => {
if (!result.length) {
res.status(200).json({ message: 'No cereal to delete! Check the name of the entered cereal.' })
} else {
cerealDB.deleteOne({ result })
res.status(200).json({ message: 'Information Deleted!' })
}`
Hello I am a beginner I may not be explaining the terms right but I appreciate any help. The code is made to have no duplicate names of cereal.
Trying to make a very simple database using mongoDB. I currently think the issue is that to try and delete it I am doing ” cerealDB.deleteOne({ result }) “, but I think I need to use the cerealInfo constant in script 1. I do not know how to access that constant though since it was created inside a function of a separate script.
There are separate routes for creating, deleting, viewing ceral. In script1 a cereal is created inside a function. In script2 I try to delete the ceral so I pass the ceral name in the url ex. /delete/cereal/cocopuffs. and the script throws no error resulting in the message “Product Deleted”. The issue is the cereal is not actually removed and is still in the database.
All the routes work and are being sent the appropriate http requests (delete for the delete script), I am using postman to send the requests.
user25086423 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.