为什么我不能将字段添加到JS对象?

时间:2013-05-04 07:26:48

标签: javascript node.js sequelize.js

我正在使用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.isExtensiblecategory对象上返回true,但我无法弄清楚如何实际扩展它

2 个答案:

答案 0 :(得分:6)

试试这个:

.success(function(category) {
  category = category.toJSON(); // convert to a simple JS object
  ...
  category.item_ids = ci;
  ...
  res.render(...);
});

答案 1 :(得分:0)