Tuesday, 11 April 2017

Express Cassandra Auto-Load Models from a Directory - models.instance.Person is not a constructor

I am basically trying to implement a person model from express-cassandra tutorial.

I have problems with auto loading model from the model folder. My model is located in /models/PersonModel.js.

module.exports = {
    fields:{
        name    : "text",
        surname : "text",
        age     : "int"
    },
    key:["name"]
}

The initialisation code is in index.js one level above. This is just copy paste from tutorial. Initialisation works well without throwing errors.

var models = require('express-cassandra');

//Tell express-cassandra to use the models-directory, and
//use bind() to load the models using cassandra configurations.
models.setDirectory( __dirname + '/models').bind(
    {
        clientOptions: {
            contactPoints: ['127.0.0.1'],
            protocolOptions: { port: 9042 },
            keyspace: 'mykeyspace',
            queryOptions: {consistency: models.consistencies.one}
        },
        ormOptions: {
            //If your keyspace doesn't exist it will be created automatically
            //using the default replication strategy provided here.
            defaultReplicationStrategy : {
                class: 'SimpleStrategy',
                replication_factor: 1
            },
            migration: 'safe',
            createKeyspace: true
        }
    },
    function(err) {
        if(err) console.log(err.message);
        else console.log(models.timeuuid());
    }
);

The problem occurs when I try to insert an entry. I get an error TypeError: models.instance.Person is not a constructor. The reason is I guess that model was not autoloaded. In the dump of models object I can see that directory is set correctly and instance models are empty.

I tried to follow the tutorial. Am I missing something? Has anyone a working example of autoloading model?



via achitaka-san

No comments:

Post a Comment