为什么茉莉节点mongoose检测不等,像预期的那样?

我正在写一个简单的应用程序,保存和查找位置。 我正在使用mongoose和茉莉节点。

用户CRUDtesting按预期工作。 但是,我个人创build了用户来testing不同的自定义validation。 我也通过清除收集和重新加载所有用户来开始testing,以确保在启动save / update / etctesting之前,所有testing数据都是好的。

对于位置,我做的是一样的,但我有几十个位置,我想用数组加载它们…并testing负载沿途,以确保它工作正常。

如果我只做一个位置,它工作正常。 不止一个,他们失败了。

我知道我错过了一些asynchronous相关的步骤在这里,但我要么search错误的条款,要么我现在太接近它看到我在这里做的根本简单的错误。

版本:

  • mongoose:3.6.16
  • 茉莉花节点:1.11.0
  • mongodb:2.4.5

细节testing…

it("creating location succeeds", function(done){ for(var locIndex in testLocations) { locations.create(testLocations[locIndex], function(err, location){ // console.log says location is undefined // unless there is only one location, then it works. expect(err ).toBeNull(); expect(location.name ).toBe(testLocations[locIndex].name); done(); }); } }); 

…和创buildfunction从一个单独的文件举行位置相关的function…

 exports.create = function(location, cb){ var newLocation = new Location(location); // console.log says we make it into here... newLocation.save(function(err, newLocation){ // ...but we never make it in here, except when there's only one location if (err) { cb(err, null); } else { cb(null, newLocation); } }); }; 

…和一些testing地点…

 var testLocations = [ { "name" : "A Great Noodle Place", "street" : "1234 Elm Street", "city" : "Springfield", "phone" : "(123) 456-7890", "website" : "n00dlesrus.com", "district" : "Downtown" }, { "name" : "Perfect Pizza Palace", "street" : "1234 Professor Ave", "city" : "Springfield" "phone" : "(321) 654-0987", "website" : "cheesegalore.com", "district" : "Uptown" } ] 

谢谢!

你在一个循环内调用done() 。 所以它在第一次迭代中被调用。 这就是为什么它只有1时才起作用。你可以尝试使用async,它会遍历一个列表,并在完成时调用最终的callback:

 it("creating location succeeds", function(done){ async.each(Object.keys(testLocation), function(key, callback){ locations.create(testLocations[key], function(err, location){ expect(err).toBeNull(); expect(location.name).toBe(testLocations[key].name); callback(); }); }, function(err) { done(); }); });