Skip to content
Advertisement

(node:31873) UnhandledPromiseRejectionWarning: TypeError: Item is not a constructor

Whenever I want to make a POST request using Postman I get this error: TypeError: Item is not a constructor. Any idea why? I have my code here:

JavaScript

And item.js:

JavaScript

Advertisement

Answer

You’re importing the full exports object, not just part of it. That object is not a constructor function (it’s not a function at all).

Looking at your exports: According to the documentation (I don’t use Mongoose), mongoose.model returns a constructor function. So:

  1. Export it using standard naming for constructor functions (ItemData, rather than itemData), and

  2. Import it rather than the entire exports object

So for instance:

JavaScript

and to import it (by destructuring it from the exports object):

JavaScript

Then, new ItemData should work.

Alternatively, import the whole thing (const item = require(/*...*/);) and then use new item.ItemData.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement