我想知道的是,当我在Typegoose类中保存一个带有许多标记为Ref的嵌套对象的新文档时,是否有一种方法可以从正确集合中的这些嵌套对象自动创建新文档。
目前,这段代码给了我一个错误:
nestedProp: CastError: Cast to ObjectId failed for value "{ Prop1: 'test1', Prop2: 'test2' }" at path "nestedProp"
import { getModelForClass, prop, Ref } from "@typegoose/typegoose";
import mongoose from "mongoose";
export class NestedClass {
@prop()
Prop1!: string;
@prop()
Prop2?: string;
}
export class MainClass {
@prop()
name!: string;
@prop({ ref: "NestedClass" })
nestedProp?: Ref<NestedClass>;
}
let exampleObject = {
name: "test",
nestedProp: {
Prop1: "test1",
Prop2: "test2",
},
};
async function test() {
await mongoose.connect("mongodb://localhost:27017/database");
const ClassModel = getModelForClass(MainClass);
try {
const u = await ClassModel.create(exampleObject);
} catch (e) {
console.log(e);
}
}
test();我知道mongoose要求对象随ObjectId一起提供,而不是对象。
然而,问题是我有许多大的类和许多嵌套的类,我试图避免循环遍历所有的大对象,以便在保存大对象之前先保存嵌套的对象。
谢谢你的回答。
发布于 2020-06-27 23:20:31
目前这是不可能的,但是Automattic/mongoose有一个关于它的问题
https://stackoverflow.com/questions/62603180
复制相似问题