【问题标题】:Mongoose .save() not working in a Job QueueMongoose .save() 不在作业队列中工作
【发布时间】:2018-08-04 21:44:18
【问题描述】:

我正在使用 DAO 将文档插入数据库。此函数正在从控制器调用,也从使用Bull 包的作业处理器调用。

dao/user.dao.js:

exports.create = function (data) {
    // Create and Save a new User
    const user = new User(data);

    return new Promise(function (resolve, reject) {
      user.save(function (error, data) {
        if (error) {
          reject(new Error(error));
        } else {
          pino.debug('Data saved to database: ' + JSON.stringify(data));
          resolve(data);
        }
      });
   });
};

user.controller.js:(这行得通……)

exports.create = function (req, res) {
  const uuid = uuidv4();
  const userDAO = require('../daos/user.dao');

  userDAO.create({
    fullName: req.body.fullName,
    email: req.body.email,
    uuid: uuid
  }).then(function (data) {
    // ... Add to mail queue & send registration link.
    res.send(data);
  }).catch(function (error) {
    res.status(500).send(// ... Send Errors);
 });
};

users-upload.processor.js:(这不起作用)

module.exports = function (job, done) {
  job.progress(0);

  const workbook = new Excel.Workbook();
  workbook.xlsx.readFile(path.join(__dirname, '../../uploads/' + job.data.filename))
    .then(() => {
      pino.debug("XLSX file is Read.");
      workbook.eachSheet(function (worksheet, sheetId) {
        worksheet.eachRow(function (row, rowNumber) {
          if (rowNumber !== 1) {
            pino.debug(`Row ${rowNumber} = ${JSON.stringify(row.values)}`);

            const uuid = uuidv4();
            const userDAO = require('../daos/user.dao');

            userDAO.create({
              fullName: row.values[1],
              email: row.values[2].text,
              uuid: uuid
            }).then(function (data) {
              // ... Add to a mail queue.
            }).catch(function (error) {
              pino.error(error);
            });
          }
        });
      });
    });

    pino.debug('Finished processing users upload...');
    job.progress(99);
    done();
  };

在这两种情况下,pre-save 钩子都会被 bieng 触发。

user.model.js:

// ... Schema Definition.

userSchema.pre('save', function (next) {
  const unverifiedUser = this;
  pino.debug(`Pre-Save Unverified User... ${JSON.stringify(this)}`);
  next();
});

这是事件的日志:

[2018-02-25T07:48:01.383Z] DEBUG (4652 on TEST-PC): Entering data upload...
[2018-02-25T07:48:01.383Z] DEBUG (4652 on TEST-PC): Request File: unverified-users-list-1519544881381.xlsx
::ffff:127.0.0.1 - POST /api/unverified-users/upload HTTP/1.1 200 50 - 181.354 ms
[2018-02-25T07:48:09.544Z] DEBUG (5864 on TEST-PC): Finished processing users upload...
[2018-02-25T07:48:09.613Z] DEBUG (5864 on TEST-PC): XLSX file is Read.
[2018-02-25T07:48:09.614Z] DEBUG (5864 on TEST-PC): Row 2 = [null,"Klark Kent",{"text":"klark.kent@justiceleague.org","hyperlink":"mailto:klark.kent@justiceleague.org"},"Teacher"]
[2018-02-25T07:48:09.615Z] DEBUG (5864 on TEST-PC): Unverified User: {"fullName":"Klark Kent","email":"klark.kent@justiceleague.org","uuid":"2f1a3c61-ad1c-443b-a5ad-8578161a5d8b"}
[2018-02-25T07:48:09.631Z] DEBUG (5864 on TEST-PC): Pre-Save Unverified User... {"name":{"first":"Klark","last":"Kent"},"_id":"5a926a3936268416e852f1de","email":"klark.kent@justiceleague.org","uuid":"2f1a3c61-ad1c-443b-a5ad-8578161a5d8b","createdAt":"2018-02-25T07:48:09.630Z","updatedAt":"2018-02-25T07:48:09.630Z","fullName":"Klark Kent","id":"5a926a3936268416e852f1de"}

我一直在搜索 google 和 stackoverflow 并尝试各种组合,但没有任何效果。如果我将处理器中的代码放入控制器中,它会完美运行。我不确定如何继续。

【问题讨论】:

    标签: node.js mongodb mongoose jobs mongoose-schema


    【解决方案1】:

    根据 issue,目前 Bull 进程中不支持 Mongoose 查询。因此,您必须使用 MongoDB 本机驱动程序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-11
      • 2018-07-15
      • 2017-01-15
      • 2019-12-29
      • 2020-11-18
      • 2011-08-26
      • 2018-06-20
      • 1970-01-01
      相关资源
      最近更新 更多