1

I am getting-started with mongodb.
I have set-up all the mongodb and the mongoose configuration and they work perfectly.
Here are the project files:

server.js:

const TableRow = require('./models/tableRow');
const bodyParser = require('body-parser');
const cors = require('cors')
const express = require('express');
const mongoose= require('mongoose')
const app = express();
const router = express.Router();
app.use(cors());
app.use(bodyParser.json());
mongoose.connect('mongodb://localhost/table', function(err) {
  if (err) { throw err; }
  console.log('Successfully connected');
});
const connection = mongoose.connection;
connection.on('error', console.error.bind(console, 'connection error:'));
connection.once('open', () => {
    console.log('MongoDB database connection established successfully!');
});
app.use('/', router);
router.route('/table/add').post((req, res) => {
  let tableRow = new TableRow (req.body);
  tableRow.save()
      .then(issue => {
          res.status(200).json({'tableRow': 'Added successfully'});
      })
      .catch(err => {
          res.status(400).send('Failed to create new record');
      });
});
app.listen(5757, () => console.log(`Express server running on port 5757`)); 

tableRow.js

const mongoose = require('mongoose')
const Schema = mongoose.Schema;
let TableRow = new Schema({
    column1Data: {
        type: String
    },
    column2Data: {
        type: String
    }
});
export default mongoose.model('TableRow', TableRow);

When I tried testing this with POSTMAN:

enter image description here

I get this error:

TypeError: TableRow is not a constructor

I think the problem is with the way I required the model in server.js:

const TableRow = require('./models/tableRow');

Someone else faced a similar issue and posted it on stackOverFlow. However, they are defining the model in the same file the server code.

Similar issue but doesn't solve my problem

So his post doesn't solve my issue.
Any idea how I can fix this?

Ahmed Ghrib
  • 695
  • 4
  • 13
  • 29

1 Answers1

1

Change from

export default mongoose.model('TableRow', TableRow);

To this:

module.exports = mongoose.model('TableRow', TableRow);

And import so: const TableRow = require('./models/tableRow');

It's CommonJS module specification which is the standard used in Node.js for working with modules (more info).

When using Node.js, including express framework, you should do it so.

module.exports is the object that's actually returned as the result of a require call.(Reference)

Remember:

You must use both import and export syntax from the same standard.


A brief summary of two module systems:

Today there are two module systems that are actively being used. CommonJS (CJS) is what Node.js has used historically. ESM (EcmaScript modules) is a newer system which has been added to the JavaScript specification. Browsers already support ES modules, and Node is adding support. (source)

Matin Sasan
  • 1,835
  • 1
  • 13
  • 26
  • Thank you! Worked perfectly! So: module.exports is the ESM system and export default is the CJS system? – Ahmed Ghrib Jun 18 '19 at 08:49
  • @AhmedGhrib I'm so happy it worked for you, really :) ... oh no, you got it confused there in the comment: it's vice-versa. ```module.exports``` is from CJS, and ```export default``` from ES. Checkout these links too: [link#1](https://stackoverflow.com/a/21117231/11330560), [link#2](https://stackoverflow.com/a/14424583/11330560), [link#3](https://stackoverflow.com/a/21117231/11330560) – Matin Sasan Jun 18 '19 at 09:09