3

我正在使用 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在对象上返回 true category,但我不知道如何实际扩展它

4

2 回答 2

9

试试这个:

.success(function(category) {
  category = category.toJSON(); // convert to a simple JS object
  ...
  category.item_ids = ci;
  ...
  res.render(...);
});
于 2013-05-04T09:01:00.283 回答
0

您可以添加计算属性:

http://www.sequelizejs.com/documentation#models-expansion

于 2013-05-04T08:11:01.260 回答