简体   繁体   English

dropCollection不会删除集合

[英]dropCollection isn't dropping the collection

mongoose 4.0.3 猫鼬4.0.3
node 0.10.22 节点0.10.22
mongod db version 3.0.1 mongod db版本3.0.1

I'm trying to drop a collection using moongoose, but it isn't working 我正在尝试使用Moongoose删除收藏集,但无法正常工作

run: function() {
    return Q(mongoose.connect('mongodb://127.0.0.1:27017/test',opts))
    .then(function(data){
        return Q(mongoose.connection.db.dropCollection('departments'))
        .then(function(data2){
            console.log('data2 is ',data2);
            return Q(true);
        }).catch(function(err){
            console.log('err is',err);
            return Q(false);
        });
    }).catch(function(err){
        console.log('err is', err);
        return Q(false);
    });
}

This returns data2 is undefined 这将返回data2 is undefined

I tried to follow the answers based on this question : Mongoose.js: remove collection or DB 我试图根据以下问题回答答案: Mongoose.js:删除集合或数据库

I think you misunderstand the way mongoose, db and Q can interact. 我认为您误解了猫鼬,db和Q交互的方式。

you can try (not tested) 您可以尝试(未经测试)

run: function() {
  return Q(mongoose.connect('mongodb://127.0.0.1:27017/test',opts).exec())
  .then(function(){
    var db = mongoose.connection.db;
    var drop = Q.nbind(db.dropCollection, db);
    return drop('departments')
           .then(function(data2){
             console.log('data2 is ',data2);
             return true;
            }).catch(function(err){
             console.log('err is',err);
             return false;
             });
}).catch(function(err){
    console.log('err is', err);
    return false;
});

} }

a mongoose function needs to be called with exec() in order to return a Promise. 为了返回Promise,需要使用exec()调用猫鼬函数。

this does not extend to the dropCollection method as far as I know so you need to de-nodeify it. 据我所知,这还没有扩展到dropCollection方法,因此您需要对其进行去节点化。

I don't think you can use Q this way. 我认为您不能以这种方式使用Q。

Q() turns anything you pass to it into a promise. Q()将您传递给它的任何东西变成一个承诺。 When you pass in a function call the way you do it, you effectively pass in that function's return value. 当您以自己的方式传递函数调用时,实际上就是传递了该函数的返回值。 In other words, just like Q(3) will resolve to 3 , Q(nodefunc(args)) will resolve to whatever nodefunc(args) returns - and in case of asynchronous functions that isn't particularly much. 换句话说,就像Q(3)可以解析为3Q(nodefunc(args))可以解析为nodefunc(args)返回的任何值-在异步函数不是特别多的情况下。

I suppose you want to use ninvoke (aka nsend ) instead. 我想您想改用ninvoke (aka nsend )。

Q.ninvoke(object, methodName, ...args) Q.ninvoke(对象,方法名,...参数)

Alias: Q.nsend 别名: Q.nsend

Calls a Node.js-style method with the given variadic arguments, returning a promise that is fulfilled if the method calls back with a result, or rejected if it calls back with an error (or throws one synchronously). 使用给定的可变参数调用Node.js风格的方法,返回一个诺言,如果该方法返回一个结果,则该诺言将被满足;如果该方法返回一个错误(或同时引发一个错误),则将被拒绝。

The mongoose API docs say that connect is a synchronous method that returns nothing. 猫鼬API文档说, connect是一个同步方法,不返回任何内容。 Its close cousin createConnection will return a connection object and might be a better fit for the use case. 它的近亲createConnection将返回一个连接对象,并且可能更适合该用例。

The following code has both - a call to Q for a synchronous method that returns an actual value, and a call to Q.nsend for an asynchronous method that works with callbacks. 以下代码同时具有-调用Q的同步方法返回实际值,以及调用Q.nsend的异步方法与回调一起使用。

run: function() {
    return Q(mongoose.createConnection('mongodb://127.0.0.1:27017/test', opts))
    .then(function(connection){
        return Q.nsend(connection.db, 'dropCollection', 'departments')
        .then(function(data){
            console.log('data is ', data);
            return data;
        });
    }).catch(function(err){
        console.log('err is', err);
    });
}

(untested code) (未经测试的代码)

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

相关问题 球没有掉进盒子里 - Ball isn't dropping into boxes 为什么Meteor Collection没有反应? - Why isn't Meteor Collection behaving reactively? 在Dropping Collection之后,在保存第一条记录时会重新进行收集,但为什么不是它的索引呢? - After Dropping Collection, Collection is re-made when saving first records, but why aren't its indexes? 我的下拉菜单没有下拉(它出现在侧面)以及如何粘在栏上 - My drop down menu isn't dropping down (its appearing sideways) & how to sticky the bar 在Meteor中删除Mongo数据库集合 - Dropping a Mongo Database Collection in Meteor 为什么我的助手集合查询没有反映在html模板中? - Why isn't my helper collection query not reflecting in the html template? MongoDB-等同于不存在一个集合的LEFT JOIN - MongoDB - Equivalent of LEFT JOIN where one collection isn't exists 为什么我的Backbone Collection重置事件未触发? - Why isn't my Backbone Collection reset event firing? 集合中的fromJS fromJSON的剔除映射在集合内不起作用? - knockout mapping fromJS fromJSON isn't working inside a collection? 如果在集合中找不到特定的字段值,则插入新文档 - Insert new document if a specific field value isn't found in the collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM