Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model.build is not a function using sequelize adapter #107

Closed
jrcastillo opened this issue Jul 8, 2017 · 4 comments
Closed

model.build is not a function using sequelize adapter #107

jrcastillo opened this issue Jul 8, 2017 · 4 comments

Comments

@jrcastillo
Copy link

jrcastillo commented Jul 8, 2017

Hi guys !, I'm currently doing a small api on express and I'm running into an error when trying using factory.create('director') or factory.build('director') on my tests.
Here is the stacktrace:

TypeError: Model.build is not a function
    at SequelizeAdapter.build (~/node_modules/factory-girl/index.js:1270:20)
    at Factory._callee2$ (~/node_modules/factory-girl/index.js:155:33)
    at tryCatch (~/node_modules/regenerator-runtime/runtime.js:65:40)
    at Generator.invoke [as _invoke] (~/node_modules/regenerator-runtime/runtime.js:303:22)
    at Generator.prototype.(anonymous function) [as next] (~/node_modules/regenerator-runtime/runtime.
js:117:21)
    at step (~/node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
    at ~/node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13
    at <anonymous>

I believe it is a problem on how I'm using the library but can't figure out why. I setup the factory guiding myself through #63 (comment) but added it into an index.js file in the directory so I could export the factory to all of the rest of the files without having to repeat the process per model definition:

const fs = require('fs');
const path = require('path');
const _ = require('lodash');

var factory = require('factory-girl');
const adapter = new factory.SequelizeAdapter();
factory = factory.factory;
factory.setAdapter(adapter);

let dir = path.join(__dirname, 'lib');

// For each file in lib directory
fs.readdirSync(dir).forEach((file) => {

  // Only process JS files
  if (file[0] !== '.' && _.endsWith(file, '.js')) {
    let basename = path.basename(file, '.js');
    let mod = require(path.join(dir, basename));

    // Call exported function with our defined factory
    mod(factory);
  }
});

module.exports = factory;

and my factory as follows:

// Common libs
const casual = require('/casuals');
const Director = require('/models/director.js');

module.exports = (factory) => {
  factory.define('director', Director, {
    name: () => casual.first_name,
    email: () => casual.email,
  });

};

any help is greatly appreciated, thanks !

@simonexmachina
Copy link
Owner

The important part here is Director, but you haven't posted the contents of /models/director.js. Also, you probably want ./models/director.js, because a file path that starts with / is in the root of your filesystem.

@jrcastillo
Copy link
Author

Sorry my bad, I thought I had posted it. Here is the content of Director.js:

'use strict';

module.exports = (sequelize, DataTypes) => {
  const director = sequelize.define('director', {
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    classMethods: {
      associate: (models) => {
        director.hasMany(models.school, {
          foreignKey: {
            allowNull: false
          }
        })
      }
    }
  })
  return director
}

@simonexmachina
Copy link
Owner

Okay, so ./models/director.js returns a function, not a constructor. So you need:

const Director = require('/models/director.js')(sequelize, DataTypes);

@jrcastillo
Copy link
Author

Thanks man ! worked like a charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants