0

I want to be able to update a specific posts data inside my mongoDB using the object id. I have already created a html form where the fields get populated with the selected post data. I can then change these values and submit the form data to http://localhost:5000/update-opportunity. In my backend I then wait for a form submission on this url. This where I am stuck I don't know how to use the id I sent with axios to update a specific object in the database.

Heres my code;

axois 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);
        });
});

getDb() is just my database connection

2 Answers 2

1

You will need to use "update" method in mongo.

Currently trying to add a record.

Try something like this

const ObjectID = require('mongodb).ObjectID;

db.collection('jobs').update(
{"_id" : req.body.id},
{$set: {
company_name: new ObjectID(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()
}});
Sign up to request clarification or add additional context in comments.

3 Comments

I think you've got the correct solution here but something isn't working. Is it that I have to convert req.body.id to an objectid?
Possibly, you might want to check network tab in CHROME, and make sure you are sending data correctly. Also you can try testing your API with POSTMAN
Yeah it was the ObjectID. Thanks for the help
1

Another possible solution if you are at the beginning of your project, you could just use à quick start : https://github.com/greathappyforest/Express-Vue-QuickStart

1 Comment

This didn't solve my problem but it is a great resource thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.