我使用Sequelize ORM为MySQL编程节点。我需要为Sequelize在查询上返回的对象添加一个新字段,但似乎不起作用。为什么我不能将字段添加到JS对象?
Category.find({
where: { id: req.params.id },
include: [Item]
}).success(function(category) {
var items = category.items;
var ci = category.items.map(function(item) { return item.id; });
delete category.items; // this works
category.item_ids = ci; // this doesn't
// category['item_ids'] = ci; // this doesn't work as well
res.send({
category: category,
items: items
});
});
Object.isExtensible
上category
对象返回true,但我想不出如何实际进行扩展
太好了,正是我需要的,谢谢! – mrvn 2013-05-04 18:49:07
谢谢,解决了我的问题。尽管'typeof(category)'返回了'object',我知道它是一个JSON,但我仍然无法添加一个字段。 – Airwavezx 2017-06-16 14:06:21
@Airwavezx'toJSON()'实际上会返回一个对象,但是一个普通的JS一个而不是一个Sequelize类实例。如果你不能解决你的问题,你应该考虑创建一个新的问题。 – robertklep 2017-06-16 14:36:55