【问题标题】:How to update values in mongoDB using Express.js and axios如何使用 Express.js 和 axios 更新 mongoDB 中的值
【发布时间】:2020-03-25 14:12:54
【问题描述】:

我希望能够使用对象 id 更新我的 mongoDB 中的特定帖子数据。我已经创建了一个 html 表单,其中的字段填充了选定的帖子数据。然后我可以更改这些值并将表单数据提交给http://localhost:5000/update-opportunity。然后在我的后端等待此 url 上的表单提交。这是我卡住的地方,我不知道如何使用我通过 axios 发送的 id 来更新数据库中的特定对象。

这是我的代码;

vue组件中axois表单提交

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

后端表单提交

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() 只是我的数据库连接

【问题讨论】:

    标签: javascript express axios


    【解决方案1】:

    您需要在 mongo 中使用“更新”方法。

    目前正在尝试添加记录。

    试试这样的

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

    【讨论】:

    • 我认为您在这里找到了正确的解决方案,但有些东西不起作用。我必须将 req.body.id 转换为 objectid 吗?
    • 您可能需要检查 CHROME 中的网络选项卡,并确保您正确发送数据。您也可以尝试使用 POSTMAN 测试您的 API
    • 是的,它是 ObjectID。感谢您的帮助
    【解决方案2】:

    另一个可能的解决方案,如果你是在你的项目的开始,你可以使用 à quick start : https://github.com/greathappyforest/Express-Vue-QuickStart

    【讨论】:

    • 这并没有解决我的问题,但它是一个很好的资源,谢谢
    猜你喜欢
    • 2021-04-09
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 2021-11-23
    相关资源
    最近更新 更多