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
2 Answers
Reset to default 9Try 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