最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

注册,UnhandledPromiseRejectionWarning,UnhandledPromiseRejectionWarning:QueryResultError:0,DeprecationWarning

运维笔记admin21浏览0评论

注册,UnhandledPromiseRejectionWarning,UnhandledPromiseRejectionWarning:QueryResultError:0,DeprecationWarning

注册,UnhandledPromiseRejectionWarning,UnhandledPromiseRejectionWarning:QueryResultError:0,DeprecationWarning

你好,我有麻烦试图找出为什么我不断收到这漫长的错误消息。我试图创建一个使用Node.js的,PG-承诺与PostgreSQL数据库的简单注册功能。

(node:79941) UnhandledPromiseRejectionWarning: QueryResultError: 0
    at new QueryResultError (/Users/Documents/Login_Reg_app/node_modules/pg-promise/lib/errors/queryResult.js:131:24)
    at Query.ctx.db.client.query (/Users/Documents/Login_Reg_app/node_modules/pg-promise/lib/query.js:209:41)
    at Query.handleReadyForQuery (/Users/Documents/Login_Reg_app/node_modules/pg/lib/query.js:125:10)
    at Connection.<anonymous> (/Users/Documents/Login_Reg_app/node_modules/pg/lib/client.js:192:19)
    at Connection.emit (events.js:180:13)
    at Socket.<anonymous> (/Users/Documents/Login_Reg_app/node_modules/pg/lib/connection.js:125:12)
    at Socket.emit (events.js:180:13)
    at addChunk (_stream_readable.js:274:12)
    at readableAddChunk (_stream_readable.js:261:11)
    at Socket.Readable.push (_stream_readable.js:218:10)
(node:79941) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:79941) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
POST /signup - - ms - -

在我的代码我试图检查用户的电子邮件在我的数据库中已经注册。如果是这样,我想提醒的电子邮件是在使用中,否则,进入到网站登记。如果我注释掉,如果用户的电子邮件地址已经被注册代码的第一部分,多数民众赞成检查,登记代码工作就好了。反之为来检查,如果用户邮箱已经在数据库的代码。问题是,当我试图让他们都为一个功能一起工作。代码炸毁;(

请参阅我的代码

app.post('/', (req, res, next) => {

  const {
    first_name,
    last_name,
    user_name,
    email,
    password
  } = req.body;

  const created_on = date;

  const saltRounds = 10;

  //If user already exist in PostgreSQL Database

  db.one('SELECT user_id, first_name, last_name, email, user_name, password, created_on FROM users WHERE email = $1', [email])
    .then(user => {
      if (user) {
        return res.json({
            message: 'Email Already Exist' + email,
            user: user
          })
          .catch(error => {
            res.json({
              error: error,
              message: 'Unable to locate Email'
            })
            console.log(error);
          })
      } else {

        bcrypt.hash(password, saltRounds, (err, hash) => {

          //If  user never registered for website then following will execute

          db.none('INSERT INTO users(first_name, last_name, user_name, password, email,created_on) VALUES($1,$2,$3,$4,$5,$6)', [first_name, last_name, user_name, hash, email, created_on])
            .then(users => {
              res.json({
                message: 'User Created on ' + date,
                userInfo: req.body
              })
            })
            .catch(error => {
              res.json({
                error: error,
                message: 'Unable to Create User'
              })
              console.log(error);

            })
        })
      }
    })
})
回答如下:

QueryResultError: 0意味着查询没有结果,但返回的行预期数量不能为零。使用.one要求查询返回一行。

所以你db.one('SELECT user_id, first_name, last_name, email, user_name, password, created_on FROM users WHERE email = $1', [email])不会为email返回结果。

它是一个UnhandledPromiseRejectionWarning因为你不处理这种情况。

db.one('SELECT user_id, first_name, last_name, email, user_name, password, created_on FROM users WHERE email = $1', [email])
    .then(user => {
      if (user) {
        // ...
      } else {
        // ...
    } /*, function(err) {} */)
    // or .catch(function(err) {}) is missing here

但是因为你的if(user)的程序逻辑预计,该查询eitherreturns具体哪一行或没有,所以你正在寻找.oneOrNone(...)而不是.one(...)。因此,最终的代码看起来类似的东西:

db.oneOrNone('SELECT user_id, first_name, last_name, email, user_name, password, created_on FROM users WHERE email = $1', [email])
    .then(user => {
      if (user) {
        return res.json({
            message: 'Email Already Exist' + email,
            user: user
          })
      } else {

        bcrypt.hash(password, saltRounds, (err, hash) => {

          //If  user never registered for website then following will execute

          db.none('INSERT INTO users(first_name, last_name, user_name, password, email,created_on) VALUES($1,$2,$3,$4,$5,$6)', [first_name, last_name, user_name, hash, email, created_on])
            .then(users => {
              res.json({
                message: 'User Created on ' + date,
                userInfo: req.body
              })
            })
            .catch(error => {
              res.json({
                error: error,
                message: 'Unable to Create User'
              })
              console.log(error);

            })
        })
      }
    })
    .catch(error => {
      res.json({
        error: error,
        message: 'Unexpected error'
      })
      console.log(error);
    })
发布评论

评论列表(0)

  1. 暂无评论