簡體   English   中英

將一種模式用於另一種模式。 獲取 TypeError:無效的架構配置

[英]Using one schema into another. Getting TypeError: Invalid schema configuration

這是我的 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中的有效類型

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

請參閱Mongoose 官方文檔的第 3 段代碼摘錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM