【问题标题】:MEAN Stack : module .exports is not a functionMEAN Stack:模块 .exports 不是函数
【发布时间】:2018-11-28 14:41:48
【问题描述】:

我的架构文件中出现错误。这是我的代码。

var mongoose = require('mongoose');

var jobListSchema = mongoose.Schema({
    companyName: String,
    jobtitle: String,
    location: String
});

const JobList = module.exports('JobList',jobListSchema);

这是我的错误:

TypeError: module.exports 不是函数 在对象。 (D:\product\project-1\models\joblist.js:9:24) 在 Module._compile (module.js:652:30) 在 Object.Module._extensions..js (module.js:663:10) 在 Module.load (module.js:565:32) 在 tryModuleLoad (module.js:505:12) 在 Function.Module._load (module.js:497:3) 在 Module.require (module.js:596:17) 在要求(内部/module.js:11:18) 在对象。 (D:\product\project-1\routes\users.js:8:17) 在 Module._compile (module.js:652:30) 在 Object.Module._extensions..js (module.js:663:10) 在 Module.load (module.js:565:32) 在 tryModuleLoad (module.js:505:12) 在 Function.Module._load (module.js:497:3) 在 Module.require (module.js:596:17) 在要求(内部/module.js:11:18)

【问题讨论】:

  • 猜你想按Schema导出模型:module.exports = mongoose.model('JobList', jobListSchema);

标签: node.js angular mongodb mean-stack


【解决方案1】:

module.exports 是一个属性,而不是一个函数

试试这个

module.exports = { 'jobList': jobListSchema };

【讨论】:

    【解决方案2】:

    好像你想导出猫鼬模型而不是架构。

    会是这样的:

    db/JobList.js

    const mongoose = require('mongoose');
    const Schema = mongoose.Schema
    
    const definition = {
        companyName: Schema.Types.String,
        jobtitle: Schema.Types.String,
        location: Schema.Types.String
    };
    
    module.exports = mongoose.model('JobList', new Schema(definition));
    

    奖金

    express 应用示例中的使用示例:

    const mongoose = require('mongoose');
    mongoose.Promise = Promise;
    
    // connection and etc... goes here
    mongoose.connect(
      'mongo://127.0.0.1:21017/dbname_here', 
      {
        config: {autoIndex: false}
      }
    );
    
    // here we define models that we want to require
    const JobList = require('./db/JobList');
    
    const express = require('express'); // installation: npm i --save express
    const app = express();
    const _ = require('lodash'); // installation: npm i --save lodash
    
    
    app.get('/vacancies', async (req, res) => {
      const query = _.pick(req.query, ['joblist', 'location', 'companyName']);
      const vacancies = await JobList.find(query).limit(20).lean();
      res.status(200).send({vacancies});
    });
    
    app.listen(8080);
    

    【讨论】:

      【解决方案3】:

      也许尝试重构它...

      var mongoose = require('mongoose');
      var Schema = mongoose.Schema;
      
      var jobListSchema = new Schema({
          companyName: String,
          jobtitle: String,
          location: String
      });
      
      module.exports = mongoose.model("NAME", jobListSchema);
      

      【讨论】:

      • 然后我收到错误,因为 const JobList = module.exports('JobList', jobListSchema); ^
      猜你喜欢
      • 2013-12-20
      • 2014-09-07
      • 2017-04-09
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 2017-04-28
      • 2015-11-08
      • 1970-01-01
      相关资源
      最近更新 更多