3

MySQL用のSequelize ORMを使用してノードをプログラミングしています。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 に答える