。
const updateTask = async (req, res) => {
const { id } = req.params;
let update = {};
if (req.body.taskTitle) update. taskTitle = req.body.taskTitle。
if (req.body.taskContent) update. taskContent = req.body.taskContent;
if (req.body.role) update. role = req.body.role。
let task = await Task.updateOne(
{ taskId: id },
{
$set: {
更新。
},
},
{ runValidators: true }
);
};
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
這是我更新資料庫中的資料的代碼
因為我試圖更新單個資料或關鍵,如果我想更新單個資料,但它沒有更新任何東西,我不知道它在哪里不作業,因為我試圖控制臺資料的資料來完美uj5u.com熱心網友回復: 。const updateTask = async (req, res) => {
const { id } = req.params;
let update = {};
if (req.body.taskTitle) update. taskTitle = req.body.taskTitle。
if (req.body.taskContent) update. taskContent = req.body.taskContent;
if (req.body.role) update. role = req.body.role。
let task = await Task.updateOne(
{ taskId: id },
{
$set: update,
},
{ runValidators: true }; {span class="hljs-attr">runValidators: 啟用。
);
};
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
你所要做的就是去掉大括號,它就會像魅力一樣作業 $set : update
uj5u.com熱心網友回復:
變數Task是什么?很抱歉,我沒有真正理解你的問題,所以你能不能把它重新表述得更清楚一些?
如果沒有,請小心,用戶可能會向你的資料庫注入東西。你應該使用mongo-sanitize腳本。
像這樣 :
- 并檢查你的id是否在正確的表格中。如果在你的資料庫中,它不是用MongoId的bjson格式。如果是這種情況,請毫不猶豫地轉換你的ID,如下所示。
const sanitize = require('mongo-sanitize')。
const mongo = require('mongodb')。
const updateTask = async(req, res)=> {
const { id } = req.params;
let update = {};
if (req.body.taskTitle) update. taskTitle = sanitize(req.body.taskTitle) 。
if (req.body.taskContent) update. taskContent = sanitize(req.body.taskContent) 。
if (req.body.role) update. role = sanitize(req.body.role) 。
let task = await Task.updateOne(
{ taskId: mongo.ObjectId(sanitize(id) }, //你可以洗掉mongo.ObjectId,如果你的id不是一個objectid.。
{
$set: {
更新。
},
},
{ runValidators: true }
);
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/319826.html
標籤:
