I am using Node js and MongoDB. I have a Customer database that I am trying to update and delete entries of. My command prompt says entries have been updated and deleted but when I view my database in MongoDB the delete and update havn't actually happened. I don't understand why my command prompt says one thing but my database says another.
My database is called Customer and below is the code for Update:
Customer.find({}).toArray(function(err,array){ result = array[Math.floor(Math.random()*(array.length))]; if (err) throw err; personToUpdate = result.firstName; //console.log(personToUpdate); }); var toUpdate = { firstName: personToUpdate }; var newTitle = names.getRandomTitle(); var newPhone = names.getRandomMobile(); var newEmail = names.getRandomFirstName() + names.getRandomSurName() + names.getRandomEmail(); var newDetails = { $set: {title: newTitle, mobileNum: newPhone, email: newEmail } }; Customer.updateOne(toUpdate, newDetails, function(err, res) { if (err) throw err; console.log("\nCustomer Updated: " + personToUpdate); console.log(""); }); Then here is my code for delete:
Customer.find({}).toArray(function(err,array){ result = array[Math.floor(Math.random()*(array.length))]; console.log(result); if (err) throw err; personToDelete = result.firstName; //console.log("Deleting: " + personToDelete); }); var toDelete = { firstName: personToDelete }; Customer.deleteOne(toDelete, function(err, obj) { if (err) throw err; console.log(personToDelete + " deleted"); https://stackoverflow.com/questions/67249113/my-deleteone-and-updateone-functions-say-they-are-working-but-they-dont-actuall April 25, 2021 at 10:07AM
没有评论:
发表评论