我有一个模型,它有一个带有嵌套字段的字段,当我尝试更新该字段时它不起作用。它仍然 remail=ns 相同。下面是进一步解释的代码。
我下面有一个模型
const mongoose = require('mongoose')
const placeSchema = new mongoose.Schema({
owner: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User'
},
typeof: {
type: String,
required: true,
enum: ["studio", "hall",
"house", "mansion",
"field", "room",
"restaurant", "school",
"church", "beach", "warehouse",
"others"
]
},
location: {
type: String,
required: true,
trim: true
},
time: {
alwaysopen: {
type: Boolean,
required: true,
default: false
},
open: {
type: String
},
close: {
type: String
}
},
idealfor: [{
type: String,
required: false,
enum: ["reception", "production",
"meeting", "performance",
"dinner", "wedding",
"party", "conference"
]
}],
rooms: {
type: Number,
required: false,
trim: true
},
toilet: {
type: Number,
required: false,
trim: true
},
price: {
type: Number,
required: true,
trim: true
},
amenities: [{
type: String,
required: false,
enum: ["electricity", "a/c",
"wifi", "sound system",
"private entrance",
"kitchen", "large table", "tv",
"green screen", "Stage",
"changing room", "makeup room",
"lounge", "soundproof"
]
}],
accessibility: [{
type: String,
required: false,
enum: ["wheelchair", "Elevator",
"on-site parking", "parking near by",
"Stairs"
]
}],
isOpen: {
type: Boolean,
required: false,
default: true
},
phonenumber: {
type: String,
required: true,
trim: true
},
}, {
timestamps: true
})
问题是,当我尝试将 time.alwaysopen
字段从 false
更新为 true
时,id 不起作用。
以下是更新路线
// Edit a place
router.patch('/api/place/:id', isVerified, async (req, res) => {
const updates = Object.keys(req.body)
const allowedUpdates = ["title", "description", "maxguest", "size", "isOpen", "rules", "rooms", "toilet", "price", "phonenumber", "location", "typeof", "time", "time.alwaysopen", "time.open", "time.close"]
const isValidOperation = updates.every((update) => allowedUpdates.includes(update))
if (!isValidOperation) {
return res.status(404).send({ error: 'Invalid updates!' })
}
try {
const place = await Place.findOne({ _id: req.params.id, owner: req.user._id })
if (!place) {
return res.status(404).send({ "message": "place does not exist" })
}
updates.forEach((update) => place[update] = req.body[update])
await place.save()
res.status(201).send({place, "message": "successfully updated" })
} catch (e) {
res.status(400).send({ "message": "failed to update" })
}
})
以下是我发出的补丁 json 请求
{
"time.alwaysopen": false
}
发送补丁请求后,time.alwaysopen
的值仍然为真,但更新字段没问题。
下面是我得到的回复
{
"place": {
"time": {
"alwaysopen": true
},
"idealfor": [
"production",
"wedding"
],
"amenities": [
"electricity",
"a/c",
"Stage"
],
"accessibility": [],
"unavailabledate": [],
"isOpen": true,
"img": true,
"_id": "604028e717314039402c2c10",
"typeof": "studio",
"location": "good news church, olufemi street, surulere, Lagos, Nigeria",
"rooms": 2,
"toilet": 2,
"price": 1000,
"phonenumber": "+2348125580085",
"owner": "603e6b494e0df045745066a7",
"createdAt": "2021-03-04T00:25:11.413Z",
"updatedAt": "2021-03-04T01:19:14.867Z",
"__v": 0,
"description": "the famous hotel",
"title": "Royal habiscus edited"
},
"message": "successfully updated"
}
请问,可能是什么问题?