Javascript 如何使用Express.js和axios更新mongoDB中的值
我希望能够使用对象id更新mongoDB中的特定post数据。我已经创建了一个html表单,在该表单中,字段将填充所选post数据。然后我可以更改这些值并将表单数据提交给。在我的后端,我然后等待这个url上的表单提交。我不知道如何使用随axios发送的id更新数据库中的特定对象 这是我的密码 vue组件中的Axis表单提交Javascript 如何使用Express.js和axios更新mongoDB中的值,javascript,express,axios,Javascript,Express,Axios,我希望能够使用对象id更新mongoDB中的特定post数据。我已经创建了一个html表单,在该表单中,字段将填充所选post数据。然后我可以更改这些值并将表单数据提交给。在我的后端,我然后等待这个url上的表单提交。我不知道如何使用随axios发送的id更新数据库中的特定对象 这是我的密码 vue组件中的Axis表单提交 axios.post('http://localhost:5000/update-opportunity', { id: this.id, company_n
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()只是我的数据库连接您需要在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()
}});
您需要在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中的网络选项卡,并确保正确发送数据。您还可以尝试使用POSTMANYeah测试您的API,因为它是ObjectID。谢谢你的帮助,我想你已经找到了正确的解决方案,但是有些东西不起作用。是否我必须将req.body.id转换为objectid?可能的话,您可能需要检查CHROME中的网络选项卡,并确保正确发送数据。您还可以尝试使用POSTMANYeah测试您的API,因为它是ObjectID。谢谢你的帮助这并没有解决我的问题,但它是一个伟大的资源谢谢这并没有解决我的问题,但它是一个伟大的资源谢谢