【问题标题】:Getting invalid schema error, when having array of mongoose schema objects当有猫鼬模式对象数组时出现无效模式错误
【发布时间】:2022-01-14 13:25:42
【问题描述】:

这是我的 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;

这是我的 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;

我收到帖子错误:

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)

我在 userAccount.js 中使用了 2 个模式 userPost.js。 有什么问题?

为什么会出现以下错误:

TypeError:无效的架构配置:model 不是数组 posts 中的有效类型

我尝试查阅以下链接,尤其是 Mongoose 官方文档的第 3 段代码摘录: https://mongoosejs.com/docs/schematypes.html#arrays

我更改了以下代码:

posts:[
        {
            type: userPost
        }
    ]

收件人:

posts:
{
    type: [userPost]
}

但仍然出现同样的错误。

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    你的架构应该如下:

     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 不需要类型声明。

    【讨论】:

    • node_modules\mongoose\lib\schema.js:984 throw new TypeError('Invalid schema configuration: ' + ^ 我收到以下错误:TypeError: Invalid schema configuration: model is not a valid在数组 posts 中输入。有关有效架构类型的列表,请参见 http://..../mongoose-schematypes。
    • 仍然出现错误?
    【解决方案2】:

    你必须记住两件事:

    1. 首先是因为您正在创建架构并且您没有更改应用程序中的架构,因此不建议使用 let。相反,请尝试将您的架构存储在 Const 变量中。
    2. “posts”是一个数组,您可以简单地将架构写入数组而不使用类型。此外,您必须在另一个架构上使用该架构。您可以通过以下方式更改代码:

    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;

    然后:

    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;

    【讨论】:

    • node_modules\mongoose\lib\schema.js:984 throw new TypeError('Invalid schema configuration: ' + ^ 我收到以下错误:TypeError: Invalid schema configuration: model is not a valid在数组 posts 中输入。有关有效架构类型的列表,请参见 http://..../mongoose-schematypes。
    • 这两个是你用的机型?没有什么与猫鼬有关的了?因为我想在沙盒中为你开发它。
    【解决方案3】:

    我不确定,但是当您引用其他模型时,您总是必须使用 mongoose 引用关键字,然后使用该模型中的信息来创建数组。

    【讨论】:

      猜你喜欢
      • 2019-10-30
      • 2020-06-24
      • 1970-01-01
      • 2015-11-08
      • 2019-08-21
      • 2016-09-02
      • 2021-11-04
      • 2019-06-09
      • 2021-04-10
      相关资源
      最近更新 更多