Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

301
Visualizações
Getting invalid schema error, when having array of mongoose schema objects

Here is my schema for 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:

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. 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

I tried consulting the following link especially the 3rd code excerpt of the official Mongoose's documentation: 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.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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.

about 4 years ago · Juan Pablo Isaza Relatório

0

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. instead, try to store your schema in a Const variable.
  2. The "posts" is an array, you can simply write your schema to an array and not use types. 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;

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda