【问题标题】:Using one schema into another. Getting TypeError: Invalid schema configuration将一种模式用于另一种模式。获取 TypeError:无效的架构配置
【发布时间】:2022-01-12 15:10:13
【问题描述】:

这是我的 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 中的有效类型

【问题讨论】:

    标签: javascript node.js mongodb express mongoose


    【解决方案1】:

    您可以直接使用posts: {type: [userPost]}

    见官方Mongoose's documentation的第三段代码摘录。

    【讨论】:

      猜你喜欢
      • 2020-08-12
      • 2011-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      相关资源
      最近更新 更多