错误module.exports mongoose

时间:2018-02-06 13:10:43

标签: node.js mongoose

我有这个Schema,当我尝试导出时,mongoose会出错。

我的架构:



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);




错误mongoose:

  

/home/lais/Documentos/Projetos/Node/flashit/node_modules/mongoose/lib/document.js:55   抛出新的ObjectParameterError(obj,' obj',' Document'); ^   ObjectParameterError:参数" obj" to Document()必须是一个对象,   得到函数(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)   在model.Document   (/home/lais/Documentos/Projetos/Node/flashit/node_modules/mongoose/lib/document.js:55:11)   在model.Model   (/home/lais/Documentos/Projetos/Node/flashit/node_modules/mongoose/lib/model.js:59:12)   在新模式   (/home/lais/Documentos/Projetos/Node/flashit/node_modules/mongoose/lib/model.js:3750:13)   在Function.model   (/home/lais/Documentos/Projetos/Node/flashit/node_modules/mongoose/lib/model.js:3748:16)   在Consign.into   (/home/lais/Documentos/Projetos/Node/flashit/node_modules/consign/lib/consign.js:239:17)   在对象。   (/home/lais/Documentos/Projetos/Node/flashit/config/server.js:36:3)at at   在Object.Module._extensions..js上的Module._compile(module.js:643:30)   (module.js:654:10)在tryModuleLoad的Module.load(module.js:556:32)   (module.js:499:12)在Function.Module._load(module.js:491:3)at at   Module.require(module.js:587:17)at require   (object / module.js:11:18)在Object。   (/home/lais/Documentos/Projetos/Node/flashit/app.js:2:11)at at   在Object.Module._extensions..js上的Module._compile(module.js:643:30)   (module.js:654:10)在tryModuleLoad的Module.load(module.js:556:32)   (module.js:499:12)在Function.Module._load(module.js:491:3)at at   启动时Function.Module.runMain(module.js:684:10)   (bootstrap_node.js:187:16)

Obs:如果我评论这一行:module.exports = mongoose.model(' Agenda',AgendaSchema);错误消失

3 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。您的架构没有问题。您可能没有正确提及您的创建功能。

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

我通过上述方法解决了这个问题。

旁注:请更改架构中name的拼写

答案 1 :(得分:0)

我只需降级到4.8.5 mongoose版本,现在就可以了。

答案 2 :(得分:0)

我遇到了同样的问题,并解决了更改结构以创建架构的问题。

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);
}