node.jsexpressnode-promisify

Create register: TypeError: Right-hand side of 'instanceof' is not callable Express and Promisify


The following code shows me the error:

TypeError: Right-hand side of 'instanceof' is not callable

There seems to be a problem here with the es6-promisify library

 const {promisify} = require("es6-promisify");

 exports.register = async (req, res, next) => {
    const user = new User({ email: req.body.email, name: req.body.name });
    const register = promisify(User.register, User);
    await register(user, req.body.password);
    res.send("all has been saved")
    next(); // pass to authController.login
  };

Solution

  • The problem might be about es6-promisify library bug. I suggest changing es6-promisify to build-in node module util.

    A new working solution should be

    const { promisify } = require('util');
    
    exports.register = async (req, res, next) => {
      const user = new User({ email: req.body.email, name: req.body.name });
      const register = promisify(User.register).bind(User);
      await register(user, req.body.password);
      next();
    }
    

    Note that just apply await in front of User.register method is not working for sure due to this method is still a normal callback function, you can see on this passport-local-mongoose test case