MEAN Stack:模块.exports不是函数

时间:2018-06-19 15:11:05

标签: node.js angular mongodb mean-stack

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

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)       在要求时(internal / 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)       在要求时(internal / module.js:11:18)

3 个答案:

答案 0 :(得分:1)

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

尝试

`

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

`

答案 1 :(得分:0)

好像您要导出猫鼬模型而不是Schema。

它将是这样的:

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

答案 2 :(得分:0)

也许可以尝试重构它...

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

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

module.exports = mongoose.model("NAME", jobListSchema);