我正在使用貓鼬并有兩個模式模型,其中一個“控制”在第一個模型中有一個參考,即“框架”模型。使用 node.js,我正在嘗試創建一個 post 方法并在 postman 中對其進行測驗,但未成功。不知道我是否以正確的方式接近這個:
框架架構:
const FrameworkSchema = new Schema({
name: {
type: String,
trim: true
},
slug: {
type: String,
slug: 'name',
unique: true
},
image: {
data: Buffer,
contentType: String
},
description: {
type: String,
trim: true
},
isActive: {
type: Boolean,
default: true
},
control:
{
type: Schema.Types.ObjectId,
ref: 'Control'
},
updated: Date,
created: {
type: Date,
default: Date.now
}
});
我的控制架構:
const ControlSchema = new Schema({
_id: {
type: Schema.ObjectId,
auto: true
},
mControlNo: {
type: String
},
sControlNo: {
type: String
},
name: {
type: String,
trim: true
},
slug: {
type: String,
slug: 'name',
unique: true
},
description: {
type: String,
trim: true
},
isApplicable: {
type: Boolean,
default: true
},
updated: Date,
created: {
type: Date,
default: Date.now
}
});
我的路由器api:
router.post(
'/add',
auth,
role.checkRole(role.ROLES.Admin),
async (req, res) => {
try {
const name = req.body.name;
const description = req.body.description;
const isActive = req.body.isActive;
const control = {
mControlNo: req.body.control.mControlNo,
sControlNo: req.body.control.sControlNo,
name: req.body.control.name,
description: req.body.control.description,
isApplicable: req.body.control.isApplicable
};
const framework = new Framework({
name,
description,
isActive,
control
});
const frameworkDoc = await framework.save();
res.status(200).json({
success: true,
message: `Framework has been added successfully!`,
framework: frameworkDoc
});
} catch (error) {
res.status(400).json({
error
// error: 'Your request could not be processed. Please try again.'
});
}
}
);
我在郵遞員中測驗時的 json 檔案:
{
"name": "NCA2",
"description": "testdescription",
"isActive": true,
"control":
{
"mControlNo": "1",
"sControlNo": "2",
"name": "controltest",
"description": "controldescription",
"isApplicable": true
}
}
我得到的回應:
{
"error": {
"errors": {
"control": {
"stringValue": "\"{\n mControlNo: '1',\n sControlNo: '2',\n name: 'controltest',\n description: 'controldescription',\n isApplicable: true\n}\"",
"kind": "ObjectId",
"value": {
"mControlNo": "1",
"sControlNo": "2",
"name": "controltest",
"description": "controldescription",
"isApplicable": true
},
"path": "control",
"reason": {}
}
},
"_message": "Framework validation failed",
"message": "Framework validation failed: control: Cast to ObjectId failed for value \"{\n mControlNo: '1',\n sControlNo: '2',\n name: 'controltest',\n description: 'controldescription',\n isApplicable: true\n}\" at path \"control\""
}
}
uj5u.com熱心網友回復:
使用類似框架的全部意義mongoose在于撰寫模型并讓框架為您檢查您發送的正文是對還是錯。您不必將身體中的每個變數分配給模型。您可以簡單地撰寫以下內容,這將節省行數:
const framework = new Framework(req.body);
(當然,假設正文已通過body-parser或其他決議器正確決議為 JSON)。
然后,您檢查description或name存在:
if (!description || !name)
但它們都不存在。req.body.descriptionandreq.body.name確實存在,可能也存在framework.description,framework.name但是descriptionorname是未定義的變數。
其余代碼看起來不錯,如果錯誤仍然存??在,請error按照其他人在評論中的建議列印出 catch 子句中的內容。
在問題中添加的代碼和 OP 所做的評論之后,我們現在有更多的元素要回答。
您有一個ValidationError來自mongoose,這意味著您輸入的欄位之一不正確。您還可以看到它來自現場control。
在您的Framework架構中,您宣告一個control如下所示的欄位:
control:
{
type: Schema.Types.ObjectId,
ref: 'Control'
},
這意味著該欄位Framework采用的是一個ObjectID,而不是像您在此處發送的物件:
const framework = new Framework({
name,
description,
isActive,
control
});
該錯誤本身是明確的:Mongoose 嘗試將您的control 物件轉換為ObjectID,當然,它失敗了。
您有 2 個解決方案:
- 您可以
Control直接在Framework架構中將架構實作為物件欄位 - 或者您在路由中創建一個單獨的
Schema物件,保存它,并control在創建Framework物件時提供 ID。
第二種解決方案可能如下所示:
const control = new Control(req.body.control);
const controlDoc = await control.save();
const framework = new Framework({...req.body, control: controlDoc._id});
const frameworkDoc = await framework.save();
第一個可能看起來像這樣:
const FrameworkSchema = new Schema({
// all your Framework schema
control:
{
mControlNo: { type: String },
sControlNo: { type: String },
name: { type: String, trim: true},
// and so on....
},
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/427375.html
