[英]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)
可以解析为3
, Q(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.