I'm using the MEAN stack and trying to save data to my MongoDB database with Mongoose. However, I keep getting the error "Movie is not a constructor" for my model. But this is how the Mongoose docs show it, so i'm not sure what i'm doing wrong..
Please help!
Movie.js
let mongoose = require('mongoose');
let MovieSchema = new mongoose.Schema({
title: String,
genre: String
});
export let Movie = mongoose.model("Movie", MovieSchema);
route.js
import express = require('express');
import mongoose = require('mongoose');
let Movie = require('../models/Movie');
let router = express.Router();
router.post('/movies', function(req, res, next) {
let new_movie = new Movie(req.body);
new_movie.save(function(err, movie) {
if(err) return next(err);
res.send(movie);
});
});
This problem happens when you forget to export your model from the model file or exports in a wrong way. your model should export like this:
let Movie = mongoose.model("Movie", MovieSchema);
module.exports=Movie;
Or in a shorthand you can simply write:
module.exports=mongoose.model('Movie',MovieSchema);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With