Usage with Sequelize
See original GitHub issueTrying to use this serializer with the ORM Sequelize. Is this possible? As of now I am receiving a the JSON API formatted json but with empty attributes.
routes/employee.js
router.get('/', function (req, res) {
models.Employee.findAll().then(function(employees) {
var json = new EmployeeSerializer(employees).serialize();
res.send(employees);
});
});
serializers/employee.js
function EmployeeSerializer(employee) {
this.serialize = function () {
return new JSONAPISerializer('employees', employee, {
topLevelLinks: { self: 'http://localhost:3000/employees' },
dataLinks: {
self: function (employee) {
return 'http://localhost:3000/employees/' + employee.id
}
},
attributes: ['firstName', 'lastName'],
});
};
}
module.exports = EmployeeSerializer;
But the output to the above is
{
"links": {
"self": "http://localhost:3000/employees"
},
"data": [
{
"type": "employees",
"id": "1",
"attributes": {},
"links": {
"self": "http://localhost:3000/employees/1"
}
}
]
}
Any idea as to why this happens? Have I made an error?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
How To Use Sequelize with Node.js and MySQL - DigitalOcean
Sequelize is a Node.js-based Object Relational Mapper that makes it easy to work with MySQL, MariaDB, SQLite, PostgreSQL databases, and more. An ...
Read more >Getting Started - Sequelize
Most of the methods provided by Sequelize are asynchronous and therefore return Promises. They are all Promises , so you can use the...
Read more >Model Querying - Basics - Sequelize
Sequelize provides various methods to assist querying your database for data.
Read more >Models Usage - Sequelize
Models Usage · Model Querying - Basics · Model Querying - Finders · Raw Queries.
Read more >Sequelize | Feature-rich ORM for modern TypeScript ...
Sequelize is a modern TypeScript and Node.js ORM for Oracle, Postgres, MySQL, MariaDB, SQLite and SQL Server, and more. Featuring solid transaction support, ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I use Sequelize often too. I think you just forgot to use
toJSON()
before passing the payload to theJSONAPISerializer
:Cheers, wanted to know if you had an eligant solution for it. Thanks again