如何在mongoose中使用自定义类型?

时间:2022-03-23 15:53:08

I have the following schema:

我有以下架构:

var Location = new Schema({
    x: {
        type: String
    },
    y: {
        type: String
    }
 },{
    _id: false
 });

 var AppSchema = new Schema({
    link: {
        type: String
    },
    location: {
        type: Location
    }
})

The above not working and I'm getting the following error:

以上不起作用,我收到以下错误:

Undefined type at `location'

“位置”处的未定义类型

1 个解决方案

#1


2  

There are specific valid types within Mongoose, found here: http://mongoosejs.com/docs/schematypes.html

Mongoose中有特定的有效类型,可在此处找到:http://mongoosejs.com/docs/schematypes.html

If I were you, I'd consider using the type: Schema.Types.Mixed type. To quote the mongoose documentation, it's literally an "anything goes" type.

如果我是你,我会考虑使用类型:Schema.Types.Mixed类型。引用mongoose文档,它实际上是“任何事情”类型。

#1


2  

There are specific valid types within Mongoose, found here: http://mongoosejs.com/docs/schematypes.html

Mongoose中有特定的有效类型,可在此处找到:http://mongoosejs.com/docs/schematypes.html

If I were you, I'd consider using the type: Schema.Types.Mixed type. To quote the mongoose documentation, it's literally an "anything goes" type.

如果我是你,我会考虑使用类型:Schema.Types.Mixed类型。引用mongoose文档,它实际上是“任何事情”类型。