简体   繁体   English

如何在Mongoose中添加收藏?

[英]How do you add a collection in Mongoose?

I'm trying to clear my Users collection before and after my tests: 我正在尝试在测试之前和之后清除我的Users集合:

before(function(done) {
  mongoose.connection.collections['users'].drop(function(err) {
    mongoose.connection.collections['users'].insert(user, done);
  });
});

after(function(done) {
  mongoose.connection.collections['users'].drop(done);
});

I also tried it without the drop in the before : 我也试了一下没有dropbefore

before(function(done) {
  // mongoose.connection.collections['users'].drop(function(err) {
    mongoose.connection.collections['users'].insert(user, done);
  // });
});


after(function(done) {
  mongoose.connection.collections['users'].drop(done);
});

I'm getting these errors: 我收到这些错误:

1) Auth API "before all" hook:
     TypeError: Cannot read property 'drop' of undefined
      at Context.<anonymous> (server/api/auth/auth.spec.js:18:45)

  2) Auth API "after all" hook:
     TypeError: Cannot read property 'drop' of undefined
      at Context.<anonymous> (server/api/auth/auth.spec.js:24:45)

My code was working before, and now it isn't. 我的代码以前工作过,现在却没有。 How can I create the collection? 如何创建收藏集?


Update: I ran db.collection.users.insert({ foo: 'bar' }) in the command line, and now it looks like the collection and document exist: 更新:我在命令行中运行了db.collection.users.insert({ foo: 'bar' }) ,现在看来集合和文档已经存在:

在此处输入图片说明

But I'm still having the same problem. 但是我仍然有同样的问题。


Update 2: This worked: 更新2:工作正常:

before(function(done) {
  // mongoose.connection.collections['users'].drop(function(err) {
  //   mongoose.connection.collections['users'].insert(user, done);
  // });
  User.remove({}).exec(function() {
    User.create(user, done);
  });
});

Now when I run the old code, it also works: 现在,当我运行旧代码时,它也可以工作:

 before(function(done) {
  mongoose.connection.collections['users'].drop(function(err) {
    mongoose.connection.collections['users'].insert(user, done);
  });
//   User.remove({}).exec(function() {
//     User.create(user, done);
//   });
});

There's something weird going on here... 这里发生了一些奇怪的事情...

The reason why it was all failing before is because the "mongoose methods" employ their own "magic" that awaits the connection to be present before actually doing anything. 之前全部失败的原因是因为“猫鼬方法”使用了自己的“魔术”,该魔术在实际执行任何操作之前等待连接出现。 The node native driver methods accesssed through the .collection accessor you are using have no such protection. 通过您正在使用的.collection访问访问的节点本机驱动程序方法没有这种保护。

Thus "until" an actual "mongoose method" has already "fired" within your scripted logic, then there is no connection and such calls will simply return undefined . 因此,在您的脚本逻辑内“直到”一个实际的“猫鼬方法”已经“触发”,然后没有连接,并且此类调用将简单地返回undefined This is why it worked for you after you acutally called one of those methods first. 这就是为什么在您首先手动调用其中一种方法后,它对您有用的原因。

The fix for this if you want to use the native driver methods is quite simple. 如果要使用本机驱动程序方法,此问题的解决方法非常简单。 Being that you are either "sure" that one of the "mongoose methods" is going to fire "first", Or you just wrap your whole script logic ( after requiring mongoose and defining the connection ) with this: 要么您“确定”其中一种“猫鼬方法”将首先触发,要么用以下命令包装整个脚本逻辑(在需要猫鼬并定义了连接之后):

mongoose.connection.on("connect", function(err) {
   // all tests and setup in here
});

This makes "sure" that the database connection has always been made before any other code that works with the database is done. 这样可以“确保”在与数据库一起使用的任何其他代码完成之前,始终已建立数据库连接。

Whilst mongoose makes efforts to "hide this away", it is good practice to implement this in both your tests and any application startup code, so you can then "ensure" that the connection is actually established before trying to work with the database connection in any other way. 虽然mongoose努力“隐藏”,但是在测试和任何应用程序启动代码中都实施此做法是一种好习惯,因此您可以在尝试使用数据库连接之前先“确保”该连接已实际建立。任何其他方式。

mongoose.connection.collections['users'] is returning undefined , and then you're trying to call drop() on it. mongoose.connection.collections['users']返回undefined ,然后尝试在其上调用drop() So you are dropping a collection that doesn't exist, because you've already dropped it - last time the after ran. 所以您要删除一个不存在的集合,因为您已经删除了它-上一次运行after

Try checking that it exists before you attempt to use it, for example: 在尝试使用它之前,请尝试检查它是否存在,例如:

var users = mongoose.connection.collections['users'];
if (users) {
    users.drop(...callback...);
}

Also, when you run db.collection.users.insert({ foo: 'bar' }) in the terminal, if the collection doesn't exist, it gets created automatically. 另外,当您在终端中运行db.collection.users.insert({ foo: 'bar' })时,如果该集合不存在,则会自动创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM