【问题标题】:Express Cassandra Auto-Load Models from a Directory - models.instance.Person is not a constructorExpress Cassandra 从目录中自动加载模型 - models.instance.Person 不是构造函数
【发布时间】:2018-09-23 04:28:00
【问题描述】:

我基本上是在尝试从express-cassandra tutorial 实现一个人模型。

我在从model 文件夹自动加载模型时遇到问题。 我的模型位于/models/PersonModel.js

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

初始化代码在index.js的上一层。这只是教程中的复制粘贴。初始化效果很好,不会引发错误。

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());
    }
);

当我尝试插入条目时出现问题。我收到一个错误TypeError: models.instance.Person is not a constructor。原因是我猜该模型没有自动加载。在模型对象的转储中,我可以看到目录设置正确且实例模型为空。

我尝试按照教程进行操作。我错过了什么吗?有人有自动加载模型的工作示例吗?

【问题讨论】:

  • 这个错误是在你没有加载模型的时候出现的,看起来你已经做的很好了,我希望你在成功连接之前保存了 Person 对象。打印后尝试保存models.timeuuid()

标签: node.js express-cassandra


【解决方案1】:

迟到总比不到好!

“bind”是异步的,“function(err) {”是回调。

将您的“插入条目”代码放在那里,它会运行良好。

即。您正在尝试在数据库初始化之前插入项目。

像这样:

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!');
  });
}

【讨论】:

  • 能否请您添加一个代码 sn-p 显示您提出的答案?查看实际代码是了解修复内容的更好方法。
  • 什么都没有,我该如何删除这条评论?!
  • 如果你在某个配置文件中添加setDirectory 并且你想稍后将用户保存在另一个地方,比如说在路由函数中?
猜你喜欢
  • 1970-01-01
  • 2017-05-18
  • 2019-03-12
  • 2018-05-24
  • 2023-01-28
  • 2022-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多