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

Models not building because they are undefined #3

Closed
ajfisher opened this issue Jan 23, 2014 · 2 comments
Closed

Models not building because they are undefined #3

ajfisher opened this issue Jan 23, 2014 · 2 comments

Comments

@ajfisher
Copy link

I'm trying to load a fixture into a sequelize object. It's very simple, a User model with a bunch of very basic string attributes. I'm not doing any associations or anything wacky there, simply trying to load one user at this point.

So my fixture looks like:

[{
  "model": "User",
  "data": {
    "firstname": "AJ",
    "surname": "Fisher"
  }
}]

When I go to load this fixture I'm getting a "cannot read "firstname" of Undefined" error. This is being thrown on Line 75 of loader.js which is this:

var assoc = Model.associations[key], val = data[key];

Tracing this through the debugger I can see why the error is happening which is that the Model at this point is just a function definition and is not a real "User" object, thus there's no associations and the key (firstname in this case) can't be found.

I can see that the model definition has been loaded as the function definition is correct.

I am probably missing something but I'd have thought that Model would have had to be instantiated in order to create a new object of that class before you can start manipulating the associations and then later the attributes and building it.

Nowhere in the code for loader.js can I see this happening so I'm trying to work out where this happens?

Any help would be appreciated as I'm trying to get one basic example working before then generating a decent sized fixture to load in.

@ajfisher
Copy link
Author

Got to the bottom of this so here's the notes for someone who comes along behind.

If you have a broken up set of models (not all defined in the one file) then you'll more than likely be using a models/index.js file to define all the sequelize stuff as well as the relationships between them. This code will also have a loader for all your models so your application can use them.

Instead of defining the models to use the way it says to do it in the readme get your returned models from the index.js file

var models = require('./models');

And then pass that as the models definition to the fixture loader. This will have all of your models defined the way the fixture loader is assuming it will come with actual objects as the models.

@oliverbenns
Copy link

I had a similar issue recently that I spent way too much time on. This is due to the way babel exports work. In short, I had to revert to module.exports as opposed to export default for my fixtures.

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