DXM
DXM

Reputation: 1249

Sequelize eager loading throws error after activator.init is called

After activator.init is called, eager loading will fail with error "TypeError: Cannot read property 'name' of undefined"

After much digging, I found out that the problem comes from "collections" module, which was nested deep in the dependency tree. When activator.init is called, it imports "styliner", which imports "q-io", which imports "collections".
The problem comes from collections/shim, which defines Array globally. This somehow breaks Sequelize eager loading.

I need to use both sequelize eager loading and activator in my project. How to I solve this problem?

const config = require('./config/config')
const Sequelize = require('sequelize');
const activator = require('activator')

const sequelize = new Sequelize(config.pgsql.database, config.pgsql.user, config.pgsql.password, {
  host: config.pgsql.host,
  dialect: 'postgres',
  operatorsAliases: false,
  // disable logging; default: console.log
  logging: false,
})

const User = sequelize.define('user', {
  name: Sequelize.STRING
})

const Network = sequelize.define('network', {
  name: Sequelize.STRING
})

User.belongsToMany(Network, { through: 'user_networks' })
Network.belongsToMany(User, { through: 'user_networks' })

sequelize.sync({force: true})
.then(() => User.create({
  name: 'janedoe',
}))
.then(jane => jane.createNetwork({name: 'social'}))
.then(() => console.log('BEFORE activator.init()'))

//eager loading is fine before activator.init is called
.then(() => User.findAll({include: [Network]}))
.then(users => users.forEach(user => console.log(user.toJSON())))
.then(() => {
  activator.init({
    user: {
      find: (id, callback) => {},
      activate: (id, callback) => {},
      setPassword: (id,password,callback) => {}
    },
    transport: null,
    templates: activator.templates.file('./'),
    from: '[email protected]',
    signkey: '',
    id: 'id'
  })
})
.then(() => console.log('AFTER activator.init()'))

//eager loading breaks after activator.init is called
.then(() => User.findAll({include: [Network]}))
.then(users => users.forEach(user => console.log(user.toJSON())))
.catch(err => console.log(err))

console output is

BEFORE activator.init()
{ id: 1,
name: 'janedoe',
created_at: 2018-01-05T17:31:39.106Z,
updated_at: 2018-01-05T17:31:39.106Z,
deleted_at: null,
version: 0,
networks:
[ { id: 1,
name: 'social',
created_at: 2018-01-05T17:31:39.118Z,
updated_at: 2018-01-05T17:31:39.118Z,
deleted_at: null,
version: 0,
user_networks: [Object] } ] }
AFTER activator.init()
TypeError: Cannot read property 'name' of undefined
at Function._validateIncludedElement (/Users/derekxm/git/mttr/app_backend/node_modules/sequelize/lib/model.js:534:41)
at options.include.options.include.map.include (/Users/derekxm/git/mttr/app_backend/node_modules/sequelize/lib/model.js:394:37)
at Array.map (native)
at Function._validateIncludedElements (/Users/derekxm/git/mttr/app_backend/node_modules/sequelize/lib/model.js:390:39)
at Promise.try.then.then (/Users/derekxm/git/mttr/app_backend/node_modules/sequelize/lib/model.js:1537:14)
at tryCatcher (/Users/derekxm/git/mttr/app_backend/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/derekxm/git/mttr/app_backend/node_modules/bluebird/js/release/promise.js:510:31)
at Promise._settlePromise (/Users/derekxm/git/mttr/app_backend/node_modules/bluebird/js/release/promise.js:567:18)
at Promise._settlePromise0 (/Users/derekxm/git/mttr/app_backend/node_modules/bluebird/js/release/promise.js:612:10)
at Promise._settlePromises (/Users/derekxm/git/mttr/app_backend/node_modules/bluebird/js/release/promise.js:691:18)
at Async._drainQueue (/Users/derekxm/git/mttr/app_backend/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/Users/derekxm/git/mttr/app_backend/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues (/Users/derekxm/git/mttr/app_backend/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:649:20)
at tryOnImmediate (timers.js:622:5)
at processImmediate [as _immediateCallback] (timers.js:594:5)

Upvotes: 0

Views: 119

Answers (1)

DXM
DXM

Reputation: 1249

collections/shim is very intrusive, if both sequelize eager loading and activator have to be used, you should fork activator and edit the source to remove Styliner.

Remove Styliner = require('styliner') in lib/mailcomposer.js
Install activator from your forked repo. This solves the eager loading problem, but since you removed styliner, you won't be able to load css for your email template.

Upvotes: 0

Related Questions