I have this sequelize controller below:
const Category = require('../models').category;
module.exports = {
list(req, res) {
return Category
.all({attributes: ['title']})
.then(categories => res.status(200).send(categories))
.catch(error => res.status(400).send(error));
}
};
Essentially, returning Category.all({attributes: ['title']})
results in this array:
[
{"title": "video"},
{"title": "gif"},
{"title": "website"}
]
I want to add the object {"title": "all"}
to the beginning of the array, by doing something like this:
const Category = require('../models').category;
module.exports = {
list(req, res) {
return Category
.all({attributes: ['title']})
.then(categories =>
categories.unshift({"title": "all"})
res.status(200).send(categories)
)
.catch(error => res.status(400).send(error));
}
};
But I know this is not exactly correct. How, when handling promises and the .then()
statement, can I edit the categories array in the .then()
statement? Thanks!
.then(categories => {
categories.unshift({"title": "all"})
res.status(200).send(categories)
})
Just need to add curly braces around the code in the then statement so it’s not an implied return.