Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

305
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda