2

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?

George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
  • 1
    This error comes when you model is not loaded, and it seems that you have done everything fine, i hope you are saving Person object before making successfully connection. try to save after print this ```models.timeuuid()``` – kartikag01 Apr 20 '17 at 16:23

1 Answers1

4

Better late than never!

"bind" is asynchronous, and "function(err) {" is the callback.

Put your "insert entry" code in there, and it will run fine.

Ie. You're trying to insert the item before the database has been initialized.

Like so:

models.setDirectory( __dirname + '/models').bind(
    {
        clientOptions: {
            contactPoints: ['127.0.0.1'],
            protocolOptions: { port: 9042 },
            keyspace: 'mykeyspace',
            queryOptions: {consistency: models.consistencies.one}
        },
        ormOptions: {
            defaultReplicationStrategy : {
                class: 'SimpleStrategy',
                replication_factor: 1
            },
            migration: 'safe'
        }
    },
    function(err) {
        if(err) throw err;
      addUser();
      console.log("err thing");

        // You'll now have a `person` table in cassandra created against the model
        // schema you've defined earlier and you can now access the model instance
        // in `models.instance.Person` object containing supported orm operations.
    }
);

function addUser(){
  var john = new models.instance.Person({
      name: "John",
      surname: "Doe",
      age: 32,
      created: Date.now()
  });
  john.save(function(err){
      if(err) {
          console.log(err);
          return;
      }
      console.log('Yuppiie!');
  });
}
Gene Knight
  • 331
  • 3
  • 9
  • 2
    could you please add a code snippet showing the answer you are proposing? seeing an actual code is much better way to understand what was fixed. – Rishi Sep 21 '18 at 18:26
  • Apropos of nothing, how do I delete this comment?! – Gene Knight Sep 22 '18 at 20:23
  • What if you add `setDirectory` in some config file and you want to save the user later in another place, let's say in a route function? – Avishay28 Mar 23 '20 at 15:30