question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage with Sequelize

See original GitHub issue

Trying 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:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
SeyZcommented, Aug 9, 2015

I use Sequelize often too. I think you just forgot to use toJSON() before passing the payload to the JSONAPISerializer:

models.Employee.findAll().then(function(employees) {
    var json = new EmployeeSerializer(employees.toJSON()).serialize();
    res.send(employees);
  });
});
0reactions
rohanthackercommented, Aug 11, 2015

Cheers, wanted to know if you had an eligant solution for it. Thanks again

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found