Search code examples
javascriptnode.jssequelize.jslodashbabeljs

Sequelize five 5, Babel seven 7: TypeError: Class constructor Model cannot be invoked without 'new'


I'm writing a new application with nodejs/express/sequelize. I use babel 7 to compile my ES6. Everything works fine with sequelize so far except when I want to use removeHook method. I get this error when I use removeHook on an instance.

/home/xxxx/PhpstormProjects/some-project/node_modules/lodas/lodash.js:15709
              var result = object(this.__wrapped__),
                           ^

TypeError: Class constructor Model cannot be invoked without 'new'
    at self.object.(anonymous function) [as removeHook] (/home/xxxx/PhpstormProjects/some-project/node_modules/lodash/lodash.js:15709:28)
    at removeHook (/home/xxxx/PhpstormProjects/some-project/src/controllers/user.js:44:22)
    at callback (/home/xxxx/PhpstormProjects/some-project/src/util/loginStrategy.js:9:18)

I know this problem is coming from Babel. I was using babel six at first, but I had the same problem. I have tried everything available on GitHub, and the best solution was the following .babelrc but still I'm getting the same error. Also, I have upgraded lodash with no effect. I tried babel-plugin-lodash with no result.

.babelrc

{
  "plugins": ["lodash"],
  "presets": [
    ["@babel/env", {
      "targets": {
        "node": "10.16.0"
      }
    }]
  ]
}

package.json

{
  "name": "xxxxxxxxxxx",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "sessionSecret=lool nodemon ./app.js --exec babel-node -e js"
  },
  "dependencies": {
    "bcrypt": "^3.0.6",
    "body-parser": "^1.19.0",
    "connect-pg-simple": "^5.0.0",
    "cookie-parser": "~1.4.4",
    "debug": "~2.6.9",
    "express": "~4.16.1",
    "express-session": "^1.16.2",
    "fs": "0.0.1-security",
    "lodash": "^4.17.11",
    "lodash-es": "^4.17.11",
    "nodemon": "^1.19.1",
    "pg": "^7.11.0",
    "sequelize": "^5.8.12",
    "sequelize-cli": "^5.5.0",
    "uuid": "^3.3.2"
  },
  "devDependencies": {
    "@babel/cli": "^7.4.4",
    "@babel/core": "^7.4.5",
    "@babel/node": "^7.4.5",
    "@babel/plugin-proposal-class-properties": "^7.4.4",
    "@babel/plugin-transform-classes": "^7.4.4",
    "@babel/plugin-transform-runtime": "^7.4.4",
    "@babel/preset-env": "^7.4.5",
    "babel-eslint": "^10.0.2",
    "babel-plugin-lodash": "^3.3.4",
    "babel-preset-latest-node": "^3.2.1",
    "eslint": "^5.16.0",
    "eslint-config-airbnb-base": "^13.1.0",
    "eslint-plugin-import": "^2.18.0"
  }
}


Node Version

v10.16.0

I have seen other answers, but they didn't solve my problem. Most of them are really old. Please let me know if you need more information.


Solution

  • I solved my own problem. It turns out that I can't use removeHook on instances. I should have used models. This is a big problem with Sequelize documentation. Here is what I found.

    Instance and class methods have been removed as of v4. See here(it's a 404) Also, your beforeUpdate hook updates the user asynchronously. You should return a promise so sequelize knows it has to wait for your hook to finish doing its task and updating the user.