在调用activator.init之后,sequelize eager loading throws出错

时间:2018-01-05 18:00:44

标签: node.js sequelize.js

调用activator.init后,急切加载将失败并显示错误“TypeError:无法读取未定义的属性'名称'

经过多次挖掘,我发现问题来自“集合”模块,它嵌套在依赖树的深处。 当调用activator.init时,它会导入“styliner”,导入“q-io”,导入“collections”。
问题来自collections / shim,它全局定义了Array。这不知何故打破了Sequelize渴望加载。

我需要在我的项目中使用sequelize eager loading和activator。如何解决这个问题?

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: 'admin@mttr.net',
    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))

控制台输出

  

在activator.init()之前   {id:1,
    名称:'janedoe',
    created_at:2018-01-05T17:31:39.106Z,
    updated_at:2018-01-05T17:31:39.106Z,
    deleted_at:null,
    版本:0,
    网络:
     [{id:1,
         名称:'social',
         created_at:2018-01-05T17:31:39.118Z,
         updated_at:2018-01-05T17:31:39.118Z,
         deleted_at:null,
         版本:0,
         user_networks:[Object]}]}
  AFTER activator.init()
  TypeError:无法读取未定义的属性“name”       在Function._validateIncludedElement(/Users/derekxm/git/mttr/app_backend/node_modules/sequelize/lib/model.js:534:41)
      在options.include.options.include.map.include(/Users/derekxm/git/mttr/app_backend/node_modules/sequelize/lib/model.js:394:37)
      在Array.map(native)
      在Function._validateIncludedElements(/Users/derekxm/git/mttr/app_backend/node_modules/sequelize/lib/model.js:390:39)
      在Promise.try.then.then(/Users/derekxm/git/mttr/app_backend/node_modules/sequelize/lib/model.js:1537:14)
      在tryCatcher(/Users/derekxm/git/mttr/app_backend/node_modules/bluebird/js/release/util.js:16:23)
      在Promise._settlePromiseFromHandler(/Users/derekxm/git/mttr/app_backend/node_modules/bluebird/js/release/promise.js:510:31)
      在Promise._settlePromise(/Users/derekxm/git/mttr/app_backend/node_modules/bluebird/js/release/promise.js:567:18)
      在Promise._settlePromise0(/Users/derekxm/git/mttr/app_backend/node_modules/bluebird/js/release/promise.js:612:10)
      在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)
      在Async._drainQueues(/Users/derekxm/git/mttr/app_backend/node_modules/bluebird/js/release/async.js:143:10)
      在Immediate.Async.drainQueues(/Users/derekxm/git/mttr/app_backend/node_modules/bluebird/js/release/async.js:17:14)
      在runCallback(timers.js:649:20)
      在tryOnImmediate(timers.js:622:5)
      at processImmediate [as _immediateCallback](timers.js:594:5)

1 个答案:

答案 0 :(得分:0)

collections / shim非常具有侵入性,如果必须使用sequelize eager loading和activator,你应该分叉activator并编辑源代码以删除Styliner。

在lib / mailcomposer.js中删除Styliner = require('styliner') 从forked repo安装激活器。这解决了急切的加载问题,但是由于您删除了样式器,因此无法为您的电子邮件模板加载css。