首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Express Cassandra自动从目录加载模型- models.instance.Person不是构造函数

Express Cassandra自动从目录加载模型- models.instance.Person不是构造函数
EN

Stack Overflow用户
提问于 2017-04-11 20:28:54
回答 1查看 1.1K关注 0票数 2

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

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

代码语言:javascript
复制
module.exports = {
    fields:{
        name    : "text",
        surname : "text",
        age     : "int"
    },
    key:["name"]
}

初始化代码在上一级的index.js中。这只是从教程中复制粘贴。初始化工作良好,没有抛出错误。

代码语言:javascript
复制
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。原因是我猜该模型不是自动加载的。在models对象的转储中,我可以看到目录设置正确,实例模型为空。

我试着按照教程去做。我是不是遗漏了什么?有没有一个自动加载模型的工作示例?

EN

回答 1

Stack Overflow用户

发布于 2018-09-21 23:35:48

晚做总比不做强!

bind是异步的,function(Err){是回调。

把你的“插入入口”代码放进去,它就会运行得很好。

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

如下所示:

代码语言:javascript
复制
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!');
  });
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43345943

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档