我正在使用DAO将文档插入数据库。使用Bull包从Controller和工作处理器调用此函数。
刀/ 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();
};
在这两种情况下,预先保存的钩子都会被解雇。
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"}
我一直在搜索谷歌和stackoverflow并尝试各种组合,没有任何作用。如果我将处理器中的代码放入控制器,它可以完美地工作。我不知道该怎么办。