创建寄存器:TypeError:'instanceof'的右侧不可调用Express和Promisify

时间:2019-12-12 01:11:59

标签: node.js express node-promisify

以下代码向我显示了错误:

  

TypeError:“ instanceof”的右侧不可调用

es6-promisify库似乎在这里有问题

 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
  };

1 个答案:

答案 0 :(得分:1)

问题可能与 es6-promisify 库错误有关。我建议将 es6-promisify 更改为内置节点模块 util

一个新的可行解决方案应该是

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();
}

请注意,由于此方法仍是正常的回调函数,因此请务必在User.register方法前面应用 await 不能正常工作,您可以在此passport-local-mongoose test case上看到