【问题标题】:Error module.exports mongoose错误 module.exports mongoose
【发布时间】:2018-07-16 13:35:14
【问题描述】:

我有这个架构,当我尝试导出时,猫鼬会出错。

我的架构:

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

错误猫鼬:

/home/lais/Documentos/Projetos/Node/flashit/node_modules/mongoose/lib/document.js:55 抛出新的 ObjectParameterError(obj, 'obj', 'Document'); ^ ObjectParameterError:Document() 的参数“obj”必须是一个对象, 得到函数 (req, res, next) { app.handle(req, res, next) } at new 对象参数错误 (/home/lais/Documentos/Projetos/Node/flashit/node_modules/mongoose/lib/error/objectParameter.js:23:11) 在模型.文档 (/home/lais/Documentos/Projetos/Node/flashit/node_modules/mongoose/lib/document.js:55:11) 在模型.模型 (/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) 在寄售.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) 在 Object.Module._extensions..js 中的 Module._compile (module.js:643:30) (module.js:654:10) 在 Module.load (module.js:556:32) 在 tryModuleLoad (module.js:499:12) 在 Function.Module._load (module.js:491:3) 在 Module.require (module.js:587:17) 在 require (内部/module.js:11:18)在对象。 (/home/lais/Documentos/Projetos/Node/flashit/app.js:2:11) 在 Object.Module._extensions..js 中的 Module._compile (module.js:643:30) (module.js:654:10) 在 Module.load (module.js:556:32) 在 tryModuleLoad (module.js:499:12) 在 Function.Module._load (module.js:491:3) 在 启动时的 Function.Module.runMain (module.js:684:10) (bootstrap_node.js:187:16)

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

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:

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

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

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

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

    【讨论】:

    • { ObjectParameterError: Document() 的参数“obj”必须是一个对象 主要是由于传递给 mongoose 的参数不是一个对象。尝试搜索您发送给他们的创建方法或参数,并检查他们是否缺少 {} 花括号。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2017-11-10
        • 2020-04-06
        • 2015-07-03
        • 1970-01-01
        • 2017-10-28
        • 2019-07-22
        • 2019-04-26
        • 2012-08-05
        • 2016-12-11
        相关资源
        最近更新 更多