我已经耗尽了我在这里和其他地方找到的所有内容。看起来其他人正在取得成功,但我所做的任何事情都不会让我看到任何虚拟领域。我已经尝试了作为领域和getterMethods无济于事。我确定我的项目中安装了最新的Sequelize版本。无法让虚拟字段在Sequelize中工作
这里是对象的定义:
var Orderitem = sequelize.define("Orderitem", {
description: { type: DataTypes.STRING(80), allowNull: false },
quantity: { type: DataTypes.DECIMAL(9,2), allowNull: false, defaultValue: 0},
unitWeight: { type: DataTypes.DECIMAL(9,2), allowNull: false, defaultValue: 0},
price: { type: DataTypes.DECIMAL(9,2), allowNull: false, defaultValue: 0},
totalPrice: {
type: DataTypes.VIRTUAL(DataTypes.DECIMAL(9,2), ['price', 'quantity']),
get() {
//return this.getDataValue('unitWeight') * this.getDataValue('quantity');
return 195.99;
}
},
totalWeight: {
type: DataTypes.VIRTUAL(DataTypes.DECIMAL(9,2), ['unitWeight', 'quantity']),
get() {
//return this.getDataValue('unitWeight') * this.getDataValue('quantity');
return 45;
}
},
}, {
/*
getterMethods: {
testFoo: function() {
return 'Foo';
},
totalPrice: function() {
return this.getDataValue('price') * this.getDataValue('quantity');
},
totalWeight: function() {
return this.getDataValue('unitWeight') * this.getDataValue('quantity');
}
},
*/
classMethods: {
associate: function(models) {
Orderitem.belongsTo(models.Order, {onDelete: 'CASCADE'})
}
}
由于所报,它试图使用属性定义与虚拟类型。我也试过只是虚拟,没有其他类型或依赖关系。为了测试,我还对示例返回值进行了硬编码。
使用getterMethods(注释掉)票价的替代方案不会更好。根据我所能找到的一切,我应该能够执行find(findOne,findAll等),这些应该作为字段与所有其他字段一起返回。前4个字段从数据库返回预期值。虚拟领域无处可寻。使用上述两种方法,我做错了什么?
检索代码:
OrderController.items = function (orderId, callback) {
models.Orderitem.findAll({
//attributes: Object.keys(models.Orderitem.attributes).concat([ 'totalPrice','totalWeight']),
where: {orderId: orderId}, raw: true
}).then(function(orderitems) {
callback(null, orderitems)
}).catch(function(error) {
callback(error, null)
});
};
有没有错误,如预期检索的项目,一切完美*除了虚拟领域。他们根本不在那里被发现。请求原始或完整对象没有区别。
似乎为我工作,你也可以粘贴你的检索代码? – drinchev
向原始文章添加检索代码 - 似乎无法在评论中很好地格式化它。 –
挖掘整个返回的对象(关闭原始数据)我在以下位置找到对我的虚拟字段的引用: orderlines [0] .__ proto__ 在此属性属性是一个包含我的虚拟字段的名称数组,是每个属性的字段 - 所有都是未定义的*除了我的虚拟字段的值,我的硬编码值分别高于 - 199.95和45。 原始设置为true,这不会通过。测试更多我也看到这些将保持未定义,如果作为他们需要的两个其他领域的产品。 –