[英]Mongoose send requested to nested schema
我不太明白為什么我的請求沒有添加到我的數據庫中。 我有嵌套對象的模式。 所以我嘗試將請求發送到 object 內部的特定 object。 結果顯示為 succes,但沒有添加任何內容。
這是架構:
const personSchema = new mongoose.Schema({
connections: {
parents: {type : Array},
children: {type : Array}
}
})
這是路由器:
router.patch('/v2/:id', getPerson, async (req, res) => {
if (req.body.connections != null) {
if (req.body.connections.parents != null) { res.person.connections.parents.push(req.body.connections.parents); }
if (req.body.connections.children != null) { res.person.connections.children.push(req.body.connections.children); }
}
try {
const updatePerson = await res.person.save();
res.status(200).json({ message: 'Success' })
} catch (error) {
res.status(400).json({ message: error.message })
}
})
這是中間件:
async function getPerson(req, res, next) {
let person;
try {
person = await Person.findById(req.params.id);
if (person === null) {
return res.status(404).json({ message: 'Cannot find the person' });
}
} catch (error) {
return res.status(500).json({ message: error.message });
}
res.person = person;
next();
}
這是請求:
PATCH http://localhost:3100/api-db/v2/62e28682cecc9120c7af9de5
Content-Type: application/json
{
"connections.parents" : "test"
}
連接已建立,數據庫中的文檔已創建。
在我看來,我可能會做錯請求。 不過,我找不到有關嵌套請求的信息。
似乎是什么問題?
附言
其他沒有嵌套的請求都滿足了……
Nvm,我只是智障:
{
"connections" : {"parents" : "test"}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.