0

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

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

1 回答 1

2

问题可能与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 测试用例中看到

于 2019-12-14T08:11:24.457 回答