0

我遇到了 Mongoose 夹具加载器的问题,我不确定出了什么问题。

当我根据以下文档加载数据时:

var data = { User: [{name: 'Alex'}, {name: 'Bob'}] };

它不加载。探索代码我看到在这个文件中有一个似乎没有被触发的 async.forEach 迭代器。创建一个简单的文件来测试我仍然无法让它正常工作。显然控制台应该打印“用户”,但它没有。有人可以阐明问题可能是什么吗?请注意,虽然我已经提出了关于异步的问题,但最终我试图让 mongoose 加载程序工作,所以我需要留在他们的代码结构中。

var async = require('async');

var data = { User: [{name: 'Alex'}, {name: 'Bob'}] };

var iterator = function(modelName, next){
  // not working
  console.log(modelName);
  next();
};

async.forEach(data, iterator, function() { });
4

1 回答 1

1

The pow-mongoose-fixtures module in the NPM repository contains a bug (see bug report).

Your code contains the same bug:

async.forEach(data, ...)

forEach() operates on arrays, but data is an object. In case of the module, it was fixed by using Object.keys() to get an array of keys. You could use it too:

async.forEach(Object.keys(data), ...);

To get mongoose-fixtures working, install the GitHub version:

npm install git://github.com/powmedia/mongoose-fixtures.git

There's a couple of changed you need to make to your code as well:

var fixtures = require('mongoose-fixtures'); // renamed from 'pow-mongoose-fixtures'
var client   = mongoose.connect(...);
...
fixtures.load(data, client); // need to pass the client object
于 2013-04-29T09:07:47.217 回答