Signup, UnhandledPromiseRejectionWarning, UnhandledPromiseRejectionWarning: QueryResultError: 0, DeprecationWarning

Signup, UnhandledPromiseRejectionWarning, UnhandledPromiseRejectionWarning: QueryResultError: 0, DeprecationWarning

您好,我无法弄清楚为什么我总是收到这么长的错误消息。我正在尝试使用 Node.js、pg-promise 和 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 = ', [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(,,,,,)', [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 表示查询没有结果,但预期的 returned 行数不能为零。使用 .one 要求查询 return 正好是一行。

所以您的 db.one('SELECT user_id, first_name, last_name, email, user_name, password, created_on FROM users WHERE email = ', [email]) 不是 return email 的结果。

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

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

但是由于您的 if(user),您的程序逻辑期望此查询恰好是 return 一行或 none,因此您正在寻找 .oneOrNone(...) 而不是.one(...)。所以你的最终代码可能看起来像这样:

db.oneOrNone('SELECT user_id, first_name, last_name, email, user_name, password, created_on FROM users WHERE email = ', [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(,,,,,)', [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);
    })