0
votes

I'm getting the error in my schema file. This is my code.

var mongoose = require('mongoose');

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

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

This is my error:

TypeError: module.exports is not a function at Object. (D:\product\project-1\models\joblist.js:9:24) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at Object. (D:\product\project-1\routes\users.js:8:17) at Module._compile (module.js:652:30) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18)

3
guess You want to export model by Schema: module.exports = mongoose.model('JobList', jobListSchema);num8er

3 Answers

1
votes

module.exports is a property, not a function

Try this

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

Seems like You want to export mongoose model not Schema.

It will be like this:

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

BONUS

usage example in express app sample:

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

Maybe try refactoring it...

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

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

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