I need help figuring out how to update a specific post's data in my mongoDB using its object id. I have created an HTML form that displays the selected post's data and allows me to make changes, then submit the updated data to http://localhost:5000/update-opportunity. In my backend, I am waiting for the form submission on this URL. However, I am stuck at using the id sent with axios to update the specific object in the database.
Below is the code I have:
Axios form submission in Vue component
axios.post('http://localhost:5000/update-opportunity', {
id: this.id,
company_name: this.company_name,
company_type: this.company_type,
lines_of_business: this.lines_of_business,
client_type: this.client_type,
contract_type: this.contract_type,
contact_name: this.contact_name,
contact_number: this.contact_number,
email_address: this.email_address,
opportunity_owner: this.opportunity_owner,
decision_maker: this.decision_maker,
annual_jobs: this.annual_jobs,
average_fee: this.average_fee,
annual_value: this.annual_value,
next_steps: this.next_steps,
due_date: this.due_date
})
.then((response) => {
console.log(response);
this.$emit('formSubmitted');
})
.catch(function (error) {
console.log(error);
});
Backend form submission
router.post('/update-opportunity', (req, res, next) => {
const db = getDb();
db
.collection('jobs')
.insertOne({
company_name: req.body.company_name,
company_type: req.body.company_type,
lines_of_business: req.body.lines_of_business,
client_type: req.body.client_type,
contract_type: req.body.contract_type,
contact_name: req.body.contact_name,
contact_number: req.body.contact_number,
email_address: req.body.email_address,
opportunity_owner: req.body.opportunity_owner,
decision_maker: req.body.decision_maker,
annual_jobs: req.body.annual_jobs,
average_fee: req.body.average_fee,
annual_value: req.body.annual_value,
next_steps: req.body.next_steps,
due_date: req.body.due_date,
date_added: new Date()
})
.then(result => {
res.status(201).send();
console.log(result);
})
.catch(err => {
console.log(err);
});
});
Note: getDb() is my database connection function.