Can't export/require a module mongoose model: TypeError: User is not a constructor

I’m trying to export a mongoose model and require it on another file. I’ve tried many things, been reading a lot, but can’t find a way, I always get an error. I don’t understand why I’m failing. This is the current state of my code:

server.js

//Defining a schema
const userSchema = new Schema({
    username: String
});

//Compiling our schema into a Model.
const User = mongoose.model('User', userSchema);

module.exports = User

controllers/allControllers.js

let User = require('../server.js');

exports.new_user_controller = (req, res) => {
    const newUser = new User({ username: req.body.username });

    newUser.save(function(err, data) {
        if (err) return console.error(err);
    });
    res.send("newUser.username: " + newUser.username);
}

And the error that I get:
TypeError: User is not a constructor

UPDATE:
This test code works, an its on the same project, so now it is more confusing:

let x = require('./server');
console.log("I'm live");
console.log(x);
const newUser = new x({ username: "banana" });
console.log(newUser);

The output:

Model { User }
{ _id: 605906a07a082d39904c3ae2, username: 'banana' }

What happens when you put all the mongoose stuff at the top of the controller, remove the imports and exports and create a new User after mongoose.model?

Hi @miku86! Well, it used to be all together in the same js file, and it was working fine, I checked on MongoDB. I decided to separate it like MVC to practice exporting/require, but along the way I broke it!

I’m having a very similar problem. In its own file I create the schema and model, and export the model, but I get an error that the model is not a constructor. Curiously, I do this with a couple of different models. One works, and the other does not.

@MarianoFarace — It’s been a month since you posted; have you found out why the model’s constructor function is not being identified? My best guess is that it has something to do the with promise being returned before the connection to the database is completed, but that’s just a guess.

Thanks,
Mark

Hi @markwilx , I did not find a way, I just left all the code on the same .js file, the way it was working before I tried to split it as MVC. Let me know if you can solve it!!

@MarianoFarace – I’ve experimented and learned a bit about this problem. I presently have the Schema in its own file and export only the schema. I do not export the model. In my executable code, I instantiate the model using the exported schema. This works.

I’ve figured out why exporting the model does not work, but I’ve not yet figured out how to fix it.

In order for the model instantiation to work, the mongoose object must have successfully connected to the database. If mongoose.connect has not completed, then mongoose.model will not work.

When I have the schema and model in a separate file, that file is loaded and executed before the mongoose.connect command completes. Thus, the model fails. I think one works and the other does not because of race conditions.

Mark

Hi @markwilx ! Thanks for your reply! I havent had that issue anylonger, maybe because I’m doing it the same way as you, but without noticing the difference. It is good to know what is going on now!!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.