mysql - Helper methods and associations with include not working on sequelize -


i can't call helper methods should have been generated sequelize when using belongsto/hasmany. , cannot eager load table. when trying run helper methods message saying there no such method, , when trying use findall include passing other table says tables not associated, please see details below:

models directory:

./models --ticket.js --category.js --index.js 

ticket.js

'use strict';  module.exports = function(sequelize, datatypes) {   var ticket = sequelize.define('ticket', {     // category_id: datatypes.integer,     subject: datatypes.string,     description: datatypes.text,     requester_name: datatypes.string,     requester_email: datatypes.string,     owner_email: datatypes.string,     status: {       type: datatypes.enum,       values: ['opened', 'in progress', 'closed']     }   }, {     underscored: true,     classmethods: {       associate: function(models) {         // associations can defined here         ticket.belongsto(models['category']);         ticket.hasmany(models['interaction']);       }     }   });    return ticket; }; 

category.js

'use strict';  module.exports = function(sequelize, datatypes) {   var category = sequelize.define('category', {     name: datatypes.string   }, {     underscored: true,     classmethods: {       associate: function(models) {         // associations can defined here         category.hasmany(models['ticket']);       }     }   });   return category; }; 

index.js

'use strict';  var fs        = require('fs'); var path      = require('path'); var sequelize = require('sequelize'); var basename  = path.basename(module.filename); var env       = process.env.node_env || 'development'; var config    = require(__dirname + '/../config/config.json')[env]; var db        = {};  if (config.use_env_variable) {   var sequelize = new sequelize(process.env[config.use_env_variable]); } else {   var sequelize = new sequelize(config.database, config.username, config.password, config); }  fs   .readdirsync(__dirname)   .filter(function(file) {     return (file.indexof('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');   })   .foreach(function(file) {     var model = sequelize['import'](path.join(__dirname, file));     db[model.name] = model;   });  object.keys(db).foreach(function(modelname) {   if (db[modelname].associate) {     db[modelname].associate(db);   } });  db.sequelize = sequelize; db.sequelize = sequelize;  module.exports = db; 

then create constants models:

const models = require('../models'),     ticket = models['ticket'],     category = models['category']; 

when trying call ticket.getcategory(), below:

ticket.findbyid(req.params.ticket_id).then((ticket) => {         ticket.getcategory().then((category)=>{             ticket['category'] = category;             res.json(ticket);         });     }); 

i get:

executing (default): select `id`, `subject`, `description`, `requester_name`, `requester_email`, `owner_email`, `status`, `created_at`, `updated_at` `tickets` `ticket` `ticket`.`id` = '1'; unhandled rejection typeerror: ticket.getcategory not function 

if try use eager loading, below:

ticket.findall({where:{id:req.params.ticket_id},include:[category]}).then((ticket) => {     res.json(ticket); }); 

i get:

unhandled rejection sequelizeeagerloadingerror: category not associated ticket! 

what doing wrong?

thanks in advance!

as said using seqielize ^4.2.0, major update api has little changes.'

in version 4 shouldn't add class methods through classmethods object, instead should this.

var ticket = sequelize.define('ticket', {     // category_id: datatypes.integer,     subject: datatypes.string,     description: datatypes.text,     requester_name: datatypes.string,     requester_email: datatypes.string,     owner_email: datatypes.string,     status: {       type: datatypes.enum,       values: ['opened', 'in progress', 'closed']     }   }, {     underscored: true, });  ticket.associate = function(models) {     // associations can defined here     ticket.belongsto(models['category']);     ticket.hasmany(models['interaction']); }; 

you can read how upgrade sequelize 3 4 here


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -