Lais Aguiar
Lais Aguiar

Reputation: 13

Error module.exports mongoose

I have this Schema and when I try to export, the mongoose it gives an error.

My Schema:

const mongoose = require('../../config/db');

const AgendaSchema = new mongoose.Schema({

  nome: {type: String},
  adress: {type: String},
  data: {type: Date},
  createdAt: {type: Date, default: Date.now}
});

module.exports = mongoose.model('Agenda', AgendaSchema);

Error mongoose:

/home/lais/Documentos/Projetos/Node/flashit/node_modules/mongoose/lib/document.js:55 throw new ObjectParameterError(obj, 'obj', 'Document'); ^ ObjectParameterError: Parameter "obj" to Document() must be an object, got function (req, res, next) { app.handle(req, res, next) } at new ObjectParameterError (/home/lais/Documentos/Projetos/Node/flashit/node_modules/mongoose/lib/error/objectParameter.js:23:11) at model.Document (/home/lais/Documentos/Projetos/Node/flashit/node_modules/mongoose/lib/document.js:55:11) at model.Model (/home/lais/Documentos/Projetos/Node/flashit/node_modules/mongoose/lib/model.js:59:12) at new model (/home/lais/Documentos/Projetos/Node/flashit/node_modules/mongoose/lib/model.js:3750:13) at Function.model (/home/lais/Documentos/Projetos/Node/flashit/node_modules/mongoose/lib/model.js:3748:16) at Consign.into (/home/lais/Documentos/Projetos/Node/flashit/node_modules/consign/lib/consign.js:239:17) at Object. (/home/lais/Documentos/Projetos/Node/flashit/config/server.js:36:3) at Module._compile (module.js:643:30) at Object.Module._extensions..js (module.js:654:10) at Module.load (module.js:556:32) at tryModuleLoad (module.js:499:12) at Function.Module._load (module.js:491:3) at Module.require (module.js:587:17) at require (internal/module.js:11:18) at Object. (/home/lais/Documentos/Projetos/Node/flashit/app.js:2:11) at Module._compile (module.js:643:30) at Object.Module._extensions..js (module.js:654:10) at Module.load (module.js:556:32) at tryModuleLoad (module.js:499:12) at Function.Module._load (module.js:491:3) at Function.Module.runMain (module.js:684:10) at startup (bootstrap_node.js:187:16)

Obs: If I comment this line: module.exports = mongoose.model('Agenda', AgendaSchema); the error disappears

Upvotes: 1

Views: 6373

Answers (3)

qixmiers
qixmiers

Reputation: 1

I got the same problem and resolved it changing the structure to create schema.

module.exports = () => {
 const AgendaSchema = mongoose.Schema({
   nome: {type: String},
   adress: {type: String},
   data: {type: Date},
   createdAt: {type: Date, default: Date.now}
 });

 return mongoose.model('Agenda', AgendaSchema);
}

Upvotes: 0

subham agrawal
subham agrawal

Reputation: 21

I was getting the same problem. there is no problem with your schema. you may not have mentioned your create function properly.

Agenda.create({name: "subham" ,adress:"ncsdi"},callbackfunction);

I solved the problem by above method.

Sidenote: please change the spelling of your name in your schema

Upvotes: 2

GudarJS
GudarJS

Reputation: 178

I just have to downgrade to 4.8.5 mongoose version and now it's working.

Upvotes: 0

Related Questions