最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why I cannot add field to a JS object? - Stack Overflow

programmeradmin9浏览0评论

I'm programming Node with Sequelize ORM for MySQL. I need to add a new field to the object that Sequelize returns on a query, but it doesn't seems to work.

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 returns true on category object, but I can't figure out how to actually extend it

I'm programming Node with Sequelize ORM for MySQL. I need to add a new field to the object that Sequelize returns on a query, but it doesn't seems to work.

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 returns true on category object, but I can't figure out how to actually extend it

Share Improve this question asked May 4, 2013 at 7:26 marvin_yorkemarvin_yorke 3,5594 gold badges27 silver badges35 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

Try this:

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

You can add a puted property:

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

发布评论

评论列表(0)

  1. 暂无评论