将选项传递给 Sequelize 挂钩不起作用
Passing options to Sequelize hook not working
我目前正在尝试将选项传递给 Sequelize 挂钩,但是当我尝试访问挂钩内传递的选项时,它们总是未定义。
有人知道我在监督什么吗?
查询如下:
db.customer.findAll({
where: searchObject,
offset: offset,
limit: limit,
order: orderOptions
}, {
user: req.user
}).then(customers => {
// more code here
这是包含钩子定义的模型:
const db = require("../server/database");
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
module.exports = function (sequelize, Sequelize) {
var Customer = sequelize.define('customer', {
id: {
autoIncrement: true,
primaryKey: true,
allowNull: false,
type: Sequelize.INTEGER
},
salutation: {
type: Sequelize.TEXT
},
title: {
type: Sequelize.TEXT
},
firstName: {
type: Sequelize.TEXT
},
lastName: {
type: Sequelize.TEXT
}
}, {
freezeTableName: true
});
Customer.beforeFindAfterExpandIncludeAll((instance, options) => {
console.log(options);
});
return Customer;
}
我自己找到了问题的解决方案。
我完全放弃了选项部分,并采用了以下按预期工作的方法:
db.customer.findAll({
where: searchObject,
offset: offset,
limit: limit,
order: orderOptions,
user: req.user
}).then(customers => {
// more code here
Customer.beforeFindAfterExpandIncludeAll((instance) => {
console.log(instance.user); // This logs the user object correctly
});
最初的方法在其他几个 Whosebug post 中进行了演示,但似乎不再有效。
虽然此 post 中显示的方法可能会使理解传递的用户对象的目的变得有点困难,但它可以很好地完成工作。
我目前正在尝试将选项传递给 Sequelize 挂钩,但是当我尝试访问挂钩内传递的选项时,它们总是未定义。 有人知道我在监督什么吗?
查询如下:
db.customer.findAll({
where: searchObject,
offset: offset,
limit: limit,
order: orderOptions
}, {
user: req.user
}).then(customers => {
// more code here
这是包含钩子定义的模型:
const db = require("../server/database");
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
module.exports = function (sequelize, Sequelize) {
var Customer = sequelize.define('customer', {
id: {
autoIncrement: true,
primaryKey: true,
allowNull: false,
type: Sequelize.INTEGER
},
salutation: {
type: Sequelize.TEXT
},
title: {
type: Sequelize.TEXT
},
firstName: {
type: Sequelize.TEXT
},
lastName: {
type: Sequelize.TEXT
}
}, {
freezeTableName: true
});
Customer.beforeFindAfterExpandIncludeAll((instance, options) => {
console.log(options);
});
return Customer;
}
我自己找到了问题的解决方案。 我完全放弃了选项部分,并采用了以下按预期工作的方法:
db.customer.findAll({
where: searchObject,
offset: offset,
limit: limit,
order: orderOptions,
user: req.user
}).then(customers => {
// more code here
Customer.beforeFindAfterExpandIncludeAll((instance) => {
console.log(instance.user); // This logs the user object correctly
});
最初的方法在其他几个 Whosebug post 中进行了演示,但似乎不再有效。
虽然此 post 中显示的方法可能会使理解传递的用户对象的目的变得有点困难,但它可以很好地完成工作。