我正在使用Sequelize ORM for MySQL编程Node。我需要在Sequelize在查询上返回的对象中添加一个新字段,但它似乎不起作用。
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,但我无法弄清楚如何实际扩展它
答案 0 :(得分:6)
试试这个:
.success(function(category) {
category = category.toJSON(); // convert to a simple JS object
...
category.item_ids = ci;
...
res.render(...);
});
答案 1 :(得分:0)