Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Joi未处理的拒绝承诺_Javascript_Node.js_Express_Joi - Fatal编程技术网

Javascript Joi未处理的拒绝承诺

Javascript Joi未处理的拒绝承诺,javascript,node.js,express,joi,Javascript,Node.js,Express,Joi,在将Joi v13升级到^17后,下面的脚本出现问题。Joi文档声明Joi.validate已弃用,应该使用schema.validate,但这对我也不起作用。邮递员只是挂断,直到我不得不手动取消请求。以下是创建用户的post请求时的代码: const Joi = require("@hapi/joi"); const HttpStatus = require("http-status-codes"); const bcrypt = require("bcryptjs"); const jwt

在将Joi v13升级到^17后,下面的脚本出现问题。Joi文档声明Joi.validate已弃用,应该使用schema.validate,但这对我也不起作用。邮递员只是挂断,直到我不得不手动取消请求。以下是创建用户的post请求时的代码:

const Joi = require("@hapi/joi");
const HttpStatus = require("http-status-codes");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");

const User = require("../models/userModels");
const Helpers = require("../Helpers/helpers");
const dbConfig = require("../config/secret");

module.exports = {
  async CreateUser(req, res) {
    const schema = Joi.object().keys({
      username: Joi.string()
        .min(5)
        .max(10)
        .required(),
      email: Joi.string()
        .email()
        .required(),
      password: Joi.string()
        .min(5)
        .required()
    });
    const { error, value } = schema.validate(req.body, schema);
    if (error && error.details) {
      return res.status(HttpStatus.BAD_REQUEST).json({ msg: error.details });
    }

    const userEmail = await User.findOne({
      email: Helpers.lowerCase(req.body.email)
    });
    if (userEmail) {
      return res
        .status(HttpStatus.CONFLICT)
        .json({ message: "Email already exist" });
    }

    const userName = await User.findOne({
      username: Helpers.firstUpper(req.body.username)
    });
    if (userName) {
      return res
        .status(HttpStatus.CONFLICT)
        .json({ message: "Username already exist" });
    }

    return bcrypt.hash(value.password, 10, (err, hash) => {
      if (err) {
        return res
          .status(HttpStatus.BAD_REQUEST)
          .json({ message: "Error hashing password" });
      }
      const body = {
        username: Helpers.firstUpper(value.username),
        email: Helpers.lowerCase(value.email),
        password: hash
      };

      User.create(body)
        .then(user => {
          const token = jwt.sign({ data: user }, dbConfig.secret, {
            expiresIn: "5h"
          });
          res.cookie("auth", token);
          res
            .status(HttpStatus.CREATED)
            .json({ message: "User created successfully", user, token });
        })
        .catch(err => {
          res
            .status(HttpStatus.INTERNAL_SERVER_ERROR)
            .json({ message: "Error occured" });
        });
    });
  }
};
和服务器输出:

(节点:8787)未处理的PromisejectionWarning:错误:无效消息 选择权 在new module.exports(/Users/Username/chatapp/node_modules/@hapi/hoek/lib/error.js:23:19) 在module.exports(/Users/Username/chatapp/node_modules/@hapi/hoek/lib/assert.js:20:11) 在Object.exports.compile(/Users/Username/chatapp/node_modules/@hapi/joi/lib/messages.js:30:5) 在Object.exports.preferences(/Users/Username/chatapp/node_modules/@hapi/joi/lib/common.js:162:36) 在Object.exports.entry(/Users/Username/chatapp/node_modules/@hapi/joi/lib/validator.js:23:27) 在internals.Base.validate(/Users/Username/chatapp/node_modules/@hapi/joi/lib/Base.js:536:26) 在CreateUser(/Users/Username/chatapp/controllers/auth.js:25:37) 在Layer.handle[作为handle\u请求](/Users/Username/chatapp/node\u modules/express/lib/router/Layer.js:95:5) 下一步(/Users/Username/chatapp/node_modules/express/lib/router/route.js:137:13) 在Route.dispatch(/Users/Username/chatapp/node_modules/express/lib/router/Route.js:112:3) 在Layer.handle[作为handle\u请求](/Users/Username/chatapp/node\u modules/express/lib/router/Layer.js:95:5) at/Users/Username/chatapp/node_modules/express/lib/router/index.js:281:22 位于Function.process_参数(/Users/Username/chatapp/node_modules/express/lib/router/index.js:335:12) 接下来(/Users/Username/chatapp/node_modules/express/lib/router/index.js:275:10) 位于Function.handle(/Users/Username/chatapp/node_modules/express/lib/router/index.js:174:3) 在路由器上(/Users/Username/chatapp/node_modules/express/lib/router/index.js:47:12) (节点:8787)未处理的PromiserEjectionWarning:未处理的承诺 拒绝。此错误源于在异步 函数没有catch块,或者拒绝了 未使用.catch()处理。在未处理的服务器上终止节点进程 承诺拒绝,使用CLI标志
--未处理拒绝=严格
(见附件)。 (拒绝id:1)(节点:8787)[DEP0018]拒绝警告:未处理 拒绝承诺是不推荐的。在未来,承诺拒绝 未处理的将使用 非零退出代码

该脚本实际上可以在以下链接中找到:

尝试将Mongo请求包装在Try/catch中,因为如果它们抛出错误,将无法处理:

try {
  const userEmail = await User.findOne({
    email: Helpers.lowerCase(req.body.email)
  })
  if (userEmail) {
    return res
      .status(HttpStatus.CONFLICT)
      .json({ message: 'Email already exist' })
  }

  const userName = await User.findOne({
    username: Helpers.firstUpper(req.body.username)
  })
  if (userName) {
    return res
      .status(HttpStatus.CONFLICT)
      .json({ message: 'Username already exist' })
  }
} catch (err) {
  console.error(err)
}

这些是我能看到的唯一未处理的异常,所以希望它是其中之一

嘿@Jazzman,这对你有用吗?我有同样的问题,但我仍然得到同样的错误。@Nathan Jazzman的问题是一个未处理的错误,这可能意味着任何事情。可能值得发布一个带有代码示例的新问题,因为此错误不明确。如果你想的话,给我贴上标签