【问题标题】:CompoundJS: Populating database with seed file hangsCompoundJS:用种子文件填充数据库挂起
【发布时间】:2013-10-10 22:53:17
【问题描述】:

我正在尝试使用种子文件填充我的 CompoundJS 应用程序的 Mongo 数据库,但每当我运行 compound seed 时,终端在我的 console.log 语句之后挂起......数据库已填满,但我必须使用终止命令Ctrl-c。

我尝试过compound seed harvest,但这并不能创建正确的种子文件,所以我决定自己制作。这是我各自的代码:

db/seeds/development/Host.js(种子文件)

console.log("Seeding Hosts....");

var hosts = [
  {
    hid: '1',
    name: 'MY API',
    domain: 'mydomain.com'
  }
];

hosts.forEach(function(obj, err){
  Host.create(obj, function(err, host){
    console.log('host Added: ', host);
  });
});

db/schema.js

var Host = describe('Host', function () {
    property('hid', String);
    property('name', String);
    property('domain', String);
    set('restPath', pathTo.hosts);
});

config/database.js

module.exports = {
    development: {
        driver:   'mongodb',
        url:      'mongodb://localhost/apicache-dev'
    },
    test: {
        driver:   'mongodb',
        url:      'mongodb://localhost/apicache-test'
    },
    production: {
        driver:   'mongodb',
        url:      'mongodb://localhost/apicache-production'
    }
};

就像我说的,当我运行 compound seed 时,它会显示两个 console.log 语句,并将我的数据放入数据库中,但它只是挂起......实际上从未返回到命令行,所以我强制用 Ctrl-c 杀死它。我想解决这个问题,因为我必须自动化这个过程,如果它只是挂起的话,自动化有点困难。我究竟做错了什么?任何帮助将不胜感激!

Cross-posted.

编辑

所以当我尝试使用从compound seed harvest 生成的 Coffee 脚本版本时:

db/seeds/development/Host.coffee

Host.seed ->
    hid: '1'
    name: 'MY API'
    domain: 'mydomain.com'
    id: "52571edd2ac9056339000001"

我收到错误集合名称必须是字符串。所以我有点好奇,去了第 103 行的 node_modules/jugglingdb-mongodb/node_modules/mongodb/lib/mongodb/collection.js 中产生错误的地方。我放了一个console.log(collectionName) 在 if 语句之前看到了一个有趣的输出...

{ hid: '1',
  name: 'MY API',
  domain: 'mydomain.com',
  id: NaN }

很明显,它不是一个字符串,而是一个哈希对象,而且我的集合的名称(Host)根本看不到。对我来说似乎是一个错误。

【问题讨论】:

    标签: node.js mongodb seeding compoundjs


    【解决方案1】:

    所以我终于让它工作了。显然,从收获命令中自动生成的 id 有问题,所以我最终删除了该行,瞧!播下种子就像一种魅力。我将其余的 JS 文件转换为 Coffee 脚本文件,一切正常。有时您只需要在互联网上与自己对话......

    这是我的种子文件:

    Host.seed ->
      hid: '1'
      name: 'MY API'
      domain: 'mydomain.com'
    

    并且执行compound seed 不会挂起并填充数据库。猜猜咖啡脚本是要走的路?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      相关资源
      最近更新 更多