【问题标题】:How can I improve error handling on a pool如何改进池的错误处理
【发布时间】:2019-11-13 06:25:16
【问题描述】:

我创建了一个 User 类,它将保存将新用户插入 postresql 数据库的逻辑。我的代码运行良好,但我认为它写得不好,想了解如何改进它,尤其是错误处理。

const pool = require('../config/config.js');
// user constructor
class User {
  constructor(user) {
    this.username = user.username;
    this.email = user.email;
    this.password = user.password;
    this.role = user.role;
  }

  // save new user in databes
  createUser(res) {
    pool.connect((err, client, done) => {
      done();
      if (err) return res.status(400).json({ err });
      client.query('INSERT INTO users (username, email, password, role) VALUES($1, $2, $3, $4)', [this.username, this.email, this.password, this.role], (error) => {
        if (error) return res.json({ error });
        return res.json({ message: 'created successfully' });
      });
    });
  }
}

module.exports = User;

app.post('/', (req, res) => {
  const user = new User({
    username: 'Femoz',
    password: '1234',
    role: 'admin',
    email: 'femoz@gmail.com',
  });
  user.createUser(res);
  // res.json('user created successfully');
});

【问题讨论】:

  • done() 调用看起来不对。

标签: node.js postgresql express


【解决方案1】:
const pool = require('../config/config.js');

class User {
  constructor(user) {
    this.username = user.username;
    this.email = user.email;
    this.password = user.password;
    this.role = user.role;
  }

  // save new user in databes
  save(cb) {
    const user = this;

    // Perhaps, you should to do some checks of props e.g. a name is not empty
    if (!user.name)
      return cb(Error('Empty name'));

    // I think that you should implement some wrapper 
    // e.g. `db.run` to avoid call the pool directly each time. 
    pool.connect((err, client, done) => {
      // done(); here is an error. You released the db-connection too early.
      if (err) 
         return cb(err);

      // I assumed that the result of done() is undefined so cb will be called.
      // Overwise use `&&` instead `||`.
      client.query(
        'INSERT INTO users (username, email, password, role) VALUES($1, $2, $3, $4) RETURNING id', 
        [user.username, user.email, user.password, user.role], 
        (err, res) => done() || cb(err, id: res && res.rows[0].id)
      );
    });
  }
}

module.exports = User;

app.post('/', (req, res, next) => {
  const user = new User({
    username: 'Femoz',
    password: '1234',
    role: 'admin',
    email: 'femoz@gmail.com',
  });

  // Return new id is better than a static text :)
  user.save((err, id) => err ? res.status(400).json({error: err.message}) : res.json({id}));

  // OR

  // For Express you can pass error to an error handler
  user.save((err, id) => err ? next(err) : res.json({id}));
});

【讨论】:

  • "我假设 done() 的结果是未定义的" - 最好不要猜测,直接写(err, res) => { done(); cb(err, id: res && res.rows[0].id); }
  • 是的,我同意。更好:)
猜你喜欢
  • 2016-08-12
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
  • 2021-07-13
  • 1970-01-01
  • 2014-09-03
  • 2011-04-22
相关资源
最近更新 更多