【问题标题】:Mongoose schema doesn't exports to the routesMongoose 模式不会导出到路由
【发布时间】:2019-06-05 10:28:57
【问题描述】:

我想将我的架构导出到我有路由的文件,但它一直给我一个错误,上面写着:(function (exports, require, module, __filename, __dirname),我不明白。

我已经尝试过另一种导出模式的方法,例如:

module.exports = Albi = mongoose.model('albi', AlbiSchema);

但由于我在文件中有多个架构,它不断将我重新路由到另一个架构。

现在我正在使用这段代码,但它给了我一个奇怪的错误

export const Albi = mongoose.model('Albi', AlbiSchema);

我希望由此获得的结果是能够导出架构,因此我可以在我的路由文件中使用这种路由:

router.get('/', (req, res) =>{
    Albi.find({})
        .populate('category')
        .populate('area')
        .exec()
            .then(albi =>res.json(albi))
            .catch(err => res.send(err))
});

感谢您的所有提示!

【问题讨论】:

  • 当您说文件中有多个架构并且它不断将您重新路由到另一个架构时,您的意思是使用 require(this_file) 返回文件中最后导出的架构,对吗?跨度>

标签: node.js express mongoose


【解决方案1】:

您可以像这样将单个模式导出为默认模式。

const Albi = mongoose.model("Albi", AlbiSchema);
export default Albi;

并且可以导入为

import Albi from "pathToSchemaFile"

要导出多个模式,请像这样定义

module.exports = {Albi, Second, Third }

并将其导入为

const schemas = require("./pathToSchemaFile");
schemas.Albi.find({});

【讨论】:

    【解决方案2】:

    试试这个。 1.第一。 var Albi= module.exports = mongoose.model('Albi', AlbiSchema); 2.秒 var schema=require('./schema 文件路径在这里。'); 3.第三 schemas.find({});

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-16
      • 2014-10-12
      • 1970-01-01
      • 2017-07-19
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      相关资源
      最近更新 更多