Monday, 17 April 2017

Sequelizer connecting multiple hasMany associations

I am following the offical Sequelizer docs, but I am unable to get it working with multiple "hasMany" associations.

The below code renders an error: Unhandled rejection Error: [object Object] is not associated to User!

How is this done correctly?

models/user.js

module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define("User", {
    username: DataTypes.STRING
  }, {
    classMethods: {
      associate: function(models) {
        User.hasMany(models.Movie, models.Book)
      }
    }
  });

  return User;
};

routes/index.js

var models  = require('../models');
var express = require('express');
var router  = express.Router();

router.get('/', function(req, res) {
  models.User.findAll({
    include: [ models.Task, models.Movie ]
  }).then(function(users) {
    res.render('index', {
      title: 'Sequelize: Express Example',
      users: users
    });
  });
});

module.exports = router;



via Ycon

No comments:

Post a Comment