简体   繁体   English

当具有 mongoose 架构对象数组时,出现无效架构错误

[英]Getting invalid schema error, when having array of mongoose schema objects

Here is my schema for userPost.js:这是我的 userPost.js 架构:

const mongoose = require("mongoose");

let userPostSchema = new mongoose.Schema({
    id:{
        type: mongoose.Types.ObjectId
    },
   
    body:{
        type: String
    }

}, {timestamps: true});


const UserPost = mongoose.model("post", userPostSchema);

module.exports = UserPost;

Here is my schema for userAccount.js:这是我的 userAccount.js 架构:

const userPost = require("./UserPost");
const mongoose = require("mongoose");



let userAccountSchema = new mongoose.Schema({
    id:{
        type: mongoose.Types.ObjectId
    },
    
    name:{
        type: String
    },
    
    posts:
        {
            type: [userPost]
        }
    

}, {timestamps: true});

let userAccount = mongoose.model("account", userAccountSchema);

module.exports = userAccount;

I am getting the posts error:我收到帖子错误:

node_modules\mongoose\lib\schema.js:984
        throw new TypeError('Invalid schema configuration: ' +
        ^

TypeError: Invalid schema configuration: `model` is not a valid type within the array `posts`.See http://.../mongoose-schematypes for a list of valid schema types.
    at Schema.interpretAsType (C:\Users\BackEnd\node_modules\mongoose\lib\schema.js:984:15)
    at Schema.path (C:\Users\BackEnd\node_modules\mongoose\lib\schema.js:677:27)
    at Schema.add (C:\Users\BackEnd\node_modules\mongoose\lib\schema.js:495:12)

I am having 2 schemas, userPost.js, that I am using in userAccount.js.我在 userAccount.js 中使用了 2 个模式 userPost.js。 What is the problem?问题是什么?

Why am I getting the following error:为什么我会收到以下错误:

TypeError: Invalid schema configuration: model is not a valid type within the array posts TypeError:无效的架构配置: model不是数组posts中的有效类型

I tried consulting the following link especially the 3rd code excerpt of the official Mongoose's documentation: https://mongoosejs.com/docs/schematypes.html#arrays我尝试咨询以下链接,尤其是 Mongoose 官方文档的第三个代码摘录: https://mongoosejs.com/docs/schematypes.html#arrays

I changed the following code:我更改了以下代码:

posts:[
        {
            type: userPost
        }
    ]

To:至:

posts:
{
    type: [userPost]
}

but still getting the same error.但仍然得到同样的错误。

You should have your schema as follows:您的架构应该如下所示:

 const userPost = require("./UserPost");
const mongoose = require("mongoose");



let userAccountSchema = new mongoose.Schema({
    id:{
        type: mongoose.Types.ObjectId
    },
    
    name:{
        type: String
    },
    
    posts: [userPost]
        

}, {timestamps: true});

let userAccount = mongoose.model("account", userAccountSchema);

module.exports = userAccount;

posts does not need a type declaration.帖子不需要类型声明。

Two things you have to remember:你必须记住两件事:

  1. First is that because you are creating a schema and you are not changing your schema in apps, it is not recommended to use let .首先是因为您正在创建架构并且您没有更改应用程序中的架构,因此不建议使用let instead, try to store your schema in a Const variable.相反,请尝试将您的架构存储在Const变量中。
  2. The "posts" is an array, you can simply write your schema to an array and not use types. “posts”是一个数组,您可以简单地将架构写入数组而不使用类型。 Also, you must use the schema on another schema.此外,您必须在另一个架构上使用该架构。 You can change your code with:您可以使用以下方法更改代码:

 const mongoose = require("mongoose"); export const userPostSchema = new mongoose.Schema({ id:{ type: mongoose.Types.ObjectId }, body:{ type: String } }, {timestamps: true}); const UserPost = mongoose.model("post", userPostSchema); module.exports = UserPost;

and then:接着:

 import {userPostSchema} from "./UserPost"; const mongoose = require("mongoose"); const userAccountSchema = new mongoose.Schema({ id:{ type: mongoose.Types.ObjectId }, name:{ type: String }, posts: [userPostSchema] }, {timestamps: true}); let userAccount = mongoose.model("account", userAccountSchema); module.exports = userAccount;

I am not sure but this can be the case whenever you are referring to other model you always have to use the mongoose reference keyword and then use information from that model to create an array.我不确定,但每当您引用其他 model 时,您总是必须使用 mongoose 参考关键字,然后使用来自该 model 的信息来创建数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM