我使用 node.js 構建后端并將資料保存在 MongoDB 中。當我執行補丁請求時,我可以更改除日期型別之外的所有其他型別欄位的值。
這是補丁請求的后端代碼。
router.patch('/:id', isLoggedIn, async (req, res) => {
try {
const updatedBooking = await Booking.updateOne(
{_id: req.params.id},
{
$set: {userEmail: req.body.userEmail},
$set: {shiftDate: req.body.shiftDate},
$set: {isMorningShift: req.body.isMorningShift}
}
);
res.json(updatedBooking);
} catch (err) {
res.send({message: err});
}
});
這是資料庫方案:
const BookingSchema=mongoose.Schema({
userEmail:{
type:String,
required:true
},
shiftDate:{
type:Date,
required:true
},
isMorningShift:{
type: Boolean,
required: true
}
});
MongoDB 中的物件如下所示:
{
"_id": "61787183e67b6822180175f9",
"userEmail": "[email protected]",
"isMorningShift": false,
"__v": 0,
"shiftDate": "2066-06-23T00:00:00.000Z"
}
可能是什么問題?
uj5u.com熱心網友回復:
而不是 multiple $set,將所有鍵更新為一個,
const updatedBooking = await Booking.updateOne(
{_id: req.params.id},
{
$set: {
userEmail: req.body.userEmail,
shiftDate: new Date(req.body.shiftDate),
isMorningShift: req.body.isMorningShift
}
}
);
uj5u.com熱心網友回復:
@ fractal397 的答案將正常作業。如果你想要更簡潔的代碼,你可以使用它。
const bookingId = req.params.id;
const payload =
userEmail: req.body.userEmail,
shiftDate: new Date(req.body.shiftDate),
isMorningShift: req.body.isMorningShift
}
const booking = await Booking.findByIdAndUpdate(bookingId, payload);
PS - 在 Mongoose 4.0 之后,newfindByIdAndUpdate 的值已更改為false默認值。所以在這個操作中,資料庫中的資料將被更新,但它會回傳舊值booking。為了獲得更新的價值作為回應,你將不得不這樣做 -
const booking = await Booking.findByIdAndUpdate(bookingId, payload, { new : true });
uj5u.com熱心網友回復:
更改行:
$set: {shiftDate: req.body.shiftDate}
到
$set: {shiftDate: new Date(req.body.shiftDate)}
或者
$set: {shiftDate: new Date()} //for todays date in your local format
這有效: 我用 express 測驗了這個:
app.get('/updateOne', async (req, res) => {
//get data through query params in url
const id = req.query.id;
const date = req.query.date;
//connect to db and collection
//1 connect
//2 set db and collection
const client = await MongoClient.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const collection = client.db("sample_airbnb").collection("listingsAndReviews");
//update function for date field
try {
const updatedBooking = await collection.updateOne(
{_id: id},
{
$set: {name: new Date(date)} //2066-06-23T00:00:00.000Z
}
);
res.json(updatedBooking);
} catch (err) {
res.send({'message': err});
}
})
回復:
{
"acknowledged": true,
"modifiedCount": 1,
"upsertedId": null,
"upsertedCount": 0,
"matchedCount": 1
}
并更新了 Mongoscloud 中的資料:
_id
:
"100009690"
name
:
2066-06-23T00:00:00.000 00:00
我這樣稱呼端點:
http://localhost:5000/updateOne?id=100009690&date=2066-06-23T00:00:00.000Z您會看到它與您所說的期望日期格式相同。
您能否更新您的 OP 并向我們展示您傳入的確切格式?DO一console.log(req.body.shiftDate)上線7傳遞給它之前。我懷疑這里是問題所在。
顯然我不應該在名稱欄位中添加日期,但這純粹是為了快速測驗。
如果更新多個欄位,我會:
//update function
try {
const updatedBooking = await collection.updateOne(
{_id: id},
{
$set: {
name: name,
email: email,
lastShift: new Date(date)
}
}
);
res.json(updatedBooking);
} catch (err) {
res.send({'message': err});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/339450.html
