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

Cannot load Fixture #54

Closed
lawli3t opened this issue Apr 23, 2016 · 7 comments
Closed

Cannot load Fixture #54

lawli3t opened this issue Apr 23, 2016 · 7 comments

Comments

@lawli3t
Copy link

lawli3t commented Apr 23, 2016

I am currently trying to load a json file into my database, but get a strange error.

Fixtures: reading file ./app/tests/data/sampledata.json...
Unhandled rejection TypeError: Cannot convert undefined or null to object
    at Function.keys (native)
    at /vagrant/ase-2016/web/node_modules/sequelize-fixtures/lib/loader.js:114:16
    at Array.forEach (native)
    at Loader.prepFixtureData (/vagrant/ase-2016/web/node_modules/sequelize-fixtures/lib/loader.js:111:23)
    at Loader.loadFixture (/vagrant/ase-2016/web/node_modules/sequelize-fixtures/lib/loader.js:40:17)
    at null.<anonymous> (/vagrant/ase-2016/web/node_modules/sequelize-fixtures/lib/loader.js:12:21)
    at tryCatcher (/vagrant/ase-2016/web/node_modules/sequelize-fixtures/node_modules/bluebird/js/main/util.js:26:23)
    at ReductionPromiseArray._promiseFulfilled (/vagrant/ase-2016/web/node_modules/sequelize-fixtures/node_modules/bluebird/js/main/reduce.js:105:38)
    at ReductionPromiseArray.init (/vagrant/ase-2016/web/node_modules/sequelize-fixtures/node_modules/bluebird/js/main/promise_array.js:92:18)
    at ReductionPromiseArray.init (/vagrant/ase-2016/web/node_modules/sequelize-fixtures/node_modules/bluebird/js/main/reduce.js:42:10)
    at Async._drainQueue (/vagrant/ase-2016/web/node_modules/sequelize-fixtures/node_modules/bluebird/js/main/async.js:128:12)
    at Async._drainQueues (/vagrant/ase-2016/web/node_modules/sequelize-fixtures/node_modules/bluebird/js/main/async.js:133:10)
    at Immediate.Async.drainQueues [as _onImmediate] (/vagrant/ase-2016/web/node_modules/sequelize-fixtures/node_modules/bluebird/js/main/async.js:15:14)
    at processImmediate [as _immediateCallback] (timers.js:383:17)

seed_db.js

"use strict";
var sequelize_fixtures = require("sequelize-fixtures"),
    models = {
        User: require("./app/models/user"),
        LinkedService: require("./app/models/linkedServices"),
        Position: require("./app/models/position"),
        Position_Skill: require("./app/models/position_Skill"),
        Project: require("./app/models/project"),
        Rating: require("./app/models/rating"),
        Skill: require("./app/models/skill"),
        User_Skill: require("./app/models/user_Skill")
    };

sequelize_fixtures.loadFile("./app/tests/data/*.json", models).then(() => {

});

sampledata.json

[
    {
        "model": "User",
        "data": {
            "id": 2,
            "first_name": "first_name",
            "last_name": "last_name",
            "username": "testuser",
            "password": "password",
            "avatar": "path",
            "email": "email",
            "developer": true,
            "owner": false,
            "designer": false,
            "free_hours": 10,
            "avg_rating_owner": 0.4,
            "avg_rating_developer": 0.4,
            "avg_rating_designer": 0.4,
            "createdAt": "2004-10-19 10:23:54",
            "updatedAt": "2004-10-19 10:23:54"
        }
    },
    {
        "model": "LinkedServices",
        "data": {
            "id": 2,
            "user": 2,
            "token": "token1",
            "service": "GitHub"
        }
    },
   {
        "model": "Project",
        "data": {
            "id": 1,
            "UserId": 2,
            "name": "project1",
            "short_description": "project1",
            "private_description": "project1",
            "public_description": "project1",
            "budget": 10000,
            "status": "running"
        }
    },
    {
        "model": "Position",
        "data": {
            "id": 1,
            "hours": 2,
            "budget": 1000,
            "name": "position1",
            "type": "Developer",
            "ProjectId": 1,
            "status": "accepted"
        }
    },
    {
        "model": "Position",
        "data": {
            "id": 2,
            "hours": 2,
            "budget": 1000,
            "name": "position2",
            "type": "Designer",
            "UserId": 2,
            "ProjectId": 1,
            "status": "rejected"
        }
    },
    {
        "model": "Rating",
        "data": {
            "id": 1,
            "rating": 0.5,
            "feedback": "1000",
            "UserId": 2,
            "PositionId": 2
        }
    },
    {
        "model": "Skill",
        "data": {
            "id": 1,
            "name": "JAVA"
        }
    },
    {
        "model": "Skill",
        "data": {
            "id": 2,
            "name": "JPA",
            "parent_Skill_id": 1
        }
    },
    {
        "model": "User_Skill",
        "data": {
            "id": 1,
            "weight": 0.4,
            "SkillId": 1,
            "UserId": 2
        }
    },

    {
        "model": "Position_Skill",
        "data": {
            "id": 1,
            "weight": 0.4,
            "PositionId": 1,
            "SkillId": 1
        }
    }
]

Cant make any sense of it. Tried pinpointing the locations of the problem in the source code, but havent got a single clue what could even be causing this. any help greatly appreciated!

@domasx2
Copy link
Owner

domasx2 commented Apr 24, 2016

hi,

In data a model is referenced as LinkedServices, but its actually named LinkedService

Could provide a better error message for these cases, will update

@lawli3t
Copy link
Author

lawli3t commented Apr 25, 2016

The same error still persists when I change the line to:
LinkedServices: require("./app/models/linkedServices"),

I managed to make the exact same JSON work though by making it a module and exporting/importing it.

@lstanard
Copy link

Hey @lawli3t, I'm currently having this same issue. Can you provide more insight into how you got this working? I have my test data setup with module.exports, but how did you import it? Thanks!

@lawli3t
Copy link
Author

lawli3t commented May 15, 2016

here is my snippet on importing it:

const models = require("./app/models");
const fixtures = require("./app/tests/data/sampledata")

var sequelize_fixtures = require("sequelize-fixtures"),
    modelDict = {
        User: require("./app/models/user"),
        // and so on .....
    };

models.sequelize.sync( {force:true} ).then(() => {
    sequelize_fixtures.loadFixtures(fixtures, models).then(function(){
        console.dir("Data created succesfully");
    });
});

@sublet
Copy link

sublet commented Jun 8, 2016

I'm having the same issue. I even dumbed it down to just use a fixture that is a variable and I am still getting the Unhandled rejection TypeError: Cannot convert undefined or null to object error.

Any help would be greatly appreciated.

module.exports = function (sequelize, DataTypes) {
    return sequelize.define("foo", {
        propA: {type: DataTypes.STRING},
        propB: {type: DataTypes.INTEGER},
        status: {type: DataTypes.BOOLEAN}
    });
};
'use strict';

var sequelize_fixtures = require('sequelize-fixtures'),
    models = {
      Foo: require("../src/models/foo")
    };

var fixtures = [
  {
    model: 'Foo',
    data: {
      propA: 'bar',
      propB: 1
    }
  },
  {
    model: 'Foo',
    data: {
      propA: 'baz',
      propB: 3
    }
  }
];

sequelize_fixtures.loadFixtures(fixtures, models).then(function(){
  console.log('Complete!');
});

domasx2 added a commit that referenced this issue Jun 8, 2016
@domasx2
Copy link
Owner

domasx2 commented Jun 8, 2016

It finally dawned on me :)

You're passing a model factory function rather than a model.

module.exports = function (sequelize, DataTypes) { //this is a factory function, not a model
    return sequelize.define("foo", { //this produces the actual model
        propA: {type: DataTypes.STRING},
        propB: {type: DataTypes.INTEGER},
        status: {type: DataTypes.BOOLEAN}
    });
};

Instead you should import models/foo using sequelize.import(). See official example: https://github.com/sequelize/express-example/blob/master/models/index.js#L17

And your sequelize_fixtures call should look like this:

const models = require('../src/models');
sequelize_fixtures.loadFixtures(fixtures, models).then(function(){
  console.log('Complete!');
});

I released a tiny update with dac38bf that will detect this error and provide a helpful message.

I'm now thinking that sequelize-fixtures shoud ask for an instance of sequelize instead of a models map, to prevent this kind of confusion...

@domasx2 domasx2 closed this as completed Jun 8, 2016
domasx2 added a commit that referenced this issue Jun 8, 2016
@domasx2
Copy link
Owner

domasx2 commented Jun 8, 2016

realised readme was kinda misleading too, updated

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

4 participants