admin.model.ts
import mongoose, { Schema, Document } from 'mongoose';
import UserRole, { IUserRole } from './user-role.model';
export interface IAdmin extends Document {
role: IUserRole;
}
let adminSchema = new Schema<IAdmin>({
role: {
type: Schema.Types.ObjectId, ref: UserRole
}
});
export default mongoose.model<IAdmin>('Admin', adminSchema);
user-role.model.ts
import { Schema, Document, model } from 'mongoose';
export interface IUserRole extends Document{
updated_by: IAdmin|string;
}
let userRoleSchema = new Schema<IUserRole>({
updated_by: {
type: Schema.Types.ObjectId, ref: Admin
}
})
export default model<IUserRole>('UserRole', userRoleSchema);
MongooseError: Invalid ref at path "updated_by". Got undefined
at validateRef (/home/ess24/ess-smartlotto/node-rest/node_modules/mongoose/lib/helpers/populate/validateRef.js:17:9)
at Schema.path (/home/ess24/ess-smartlotto/node-rest/node_modules/mongoose/lib/schema.js:655:5)
at Schema.add (/home/ess24/ess-smartlotto/node-rest/node_modules/mongoose/lib/schema.js:535:14)
at require (internal/modules/cjs/helpers.js:88:18)
[ERROR] 22:25:54 MongooseError: Invalid ref at path "updated_by". Got undefined
以下是我的两个模型--如何解决这类问题以及如何处理循环依赖关系?
发布于 2021-11-18 23:17:50
您必须将UserRole
和Admin
的UserRole
值放在''
中,如下所示:
const adminSchema = new Schema<IAdmin>({
role: {
type: Schema.Types.ObjectId, ref: 'UserRole' // Name of the model you are referencing
}
});
let userRoleSchema = new Schema<IUserRole>({
updated_by: {
type: Schema.Types.ObjectId, ref: 'Admin' // <- and here
}
})
发布于 2022-09-29 18:57:07
虽然@gabriel-lupu的上述回答是正确的,但在较新的版本中,似乎另一个修复方法是推荐。
Fix:
ref: 'UserRole'
//推荐的第一个修复ref: () => UserRole
Typegoose文档说明如下:
ref和type选项也可以在不使用() =>的情况下定义,但通常建议与之一起使用。如果不使用() =>,则在需要类(/变量)之后定义类(/变量)时可能会出现问题。由于参考文献一节。类在运行时加载和重新排序,这可能导致某些引用为null /未定义/不存在。这就是Typegoose提供以下内容的原因:
class Nested {
@prop()
public someNestedProperty: string;
}
// Recommended first fix:
class Main {
@prop({ ref: () => Nested }) // since 7.1 arrow functions can be used to
defer getting the type
public nested: Ref<Nested>;
}
// Not recommended workaround (hardcoding model name):
class Main {
@prop({ ref: 'Nested' }) // since 7.0 it is recommended to use "console.log(getName(Class))" to get the generated name once and hardcode it like shown here
public nested: Ref<Nested>;
}
Explanation:
MongooseError:路径"updated_by“处的无效引用。未定义的错误似乎是循环依赖造成的。
IAdmin
有一个引用IUserRole
的role
属性。IUserRole
有一个引用IAdmin
的updated_by
属性。admin.model.ts
加载时,它会导入user-role.model.ts
user-role.model.ts
需要来自admin.model.ts
的updated_by
属性的IAdmin
admin.model.ts
还没有完成加载,因此导致了引用它的updated_by
未定义。Mozilla对模块如何处理循环依赖的相关解释。
在循环依赖项中,最终在图中有一个循环。通常,这是一个很长的循环。但是为了解释这个问题,我将使用一个带有短循环的人为的例子。
一个复杂的模块图,在左边有一个4个模块循环。一个简单的2模块循环在右边。 让我们看看如何与CommonJS模块一起工作。首先,主模块将执行到require语句。然后它将去加载计数器模块。
一个commonJS模块,在请求语句之后从main.js导出一个变量到依赖于该导入的counter.js 然后,计数器模块将尝试从导出对象访问消息。但是,由于尚未在主模块中对其进行评估,这将返回未定义的结果。JS引擎将为局部变量分配内存空间,并将值设置为未定义的值。
消息变量将被初始化并添加到内存中。但是,由于两者之间没有连接,所以它将在所需模块中保持未定义。
如果使用活动绑定处理导出,计数器模块最终会看到正确的值。在超时运行时,main.js的评估应该已经完成并填充了值。模块导入的工作方式
两者都有?修复似乎创建了一个活绑定。
闭包也可以关闭导入的值,这些值被视为活动绑定,因为当原始值发生变化时,导入的值也相应地发生变化。
// closureCreator.js
import { x } from "./myModule.js";
export const getX = () => x; // Close over an imported live binding
https://stackoverflow.com/questions/68167071
复制相似问题