我正在使用 typegoose,我的Query和QueryRule模型如下。
export class Query {
@prop()
queryRule: Ref<QueryRule>;
}
export class QueryRule {
@prop()
condition: Condition;
@prop()
rules: Ref<QueryRule | ChildRule>[];
}
我正在使用以下查詢進行填充。
await QueryModel.findById(queryId).populate('queryRule');
我的查詢檔案如下。
{"_id":{"$oid":"6283d73baffa60c38af8c2f0"},"name":"test","queryRule":{"$oid":"6287c8f0e3ab4b5dd7238ef3"}}
我的 QueryRule 檔案如下。
{"_id":{"$oid":"6287c8f0e3ab4b5dd7238ef3"},"condition":1,"rules":[],"__v":0}
但是當我使用填充查詢訪問 QueryRulecondition時。rules即使該檔案中存在值,我也未定義。
我在這里做錯了什么?
uj5u.com熱心網友回復:
您的問題似乎是您缺少所需的選項ref,請參閱Typegoose 檔案中的參考其他類。
在您的情況下,您的代碼應該看起來更像:
export class Query {
@prop({ ref: () => QueryRule })
queryRule: Ref<QueryRule>;
}
export class QueryRule {
@prop()
condition: Condition;
//@prop({ ref: () => QueryRule })
//rules: Ref<QueryRule | ChildRule>[];
}
至于Ref<QueryRule | ChildRule>[],您要么必須將其限制為 2 種可能性中的一種,要么使用鑒別器,請參閱Typegoose 檔案中的非嵌套鑒別器。
另外作為一個小旁注,如果您condition: Condition不是 typegoose 類,它將變為Mixed,這基本上是mongooseany運行時的型別。
uj5u.com熱心網友回復:
@prop({
ref: () => QueryRule | ChildRule,
required: true
})
rules: Ref<QueryRule | ChildRule>[];
uj5u.com熱心網友回復:
為此,我需要兩件事。
- 首先是我失蹤了
{ ref: () => QueryRule } - 然后在填充中我必須像這樣傳遞模型
{ path : 'queryRule', model: QueryRuleModel }
export class Query {
@prop({ ref: () => QueryRule })
queryRule: Ref<QueryRule>;
}
let query: Query = await QueryModel
.findById(queryId)
.populate({ path : 'queryRule', model: QueryRuleModel });
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/481735.html
