dropCollection 没有删除集合
dropCollection isn't dropping the collection
猫鼬 4.0.3
节点 0.10.22
mongod 数据库版本 3.0.1
我正在尝试使用 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);
});
}
这个returnsdata2 is undefined
我试着按照这个问题的答案:Mongoose.js: remove collection or DB
我认为你误解了 mongoose、db 和 Q 的交互方式。
你可以试试(未测试)
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;
});
}
需要使用 exec()
调用 mongoose 函数才能 return Promise。
据我所知,这不会扩展到 dropCollection 方法,因此您需要对其进行去节点化。
我不认为你可以这样使用 Q。
Q()
将您传递给它的任何内容都变成一个承诺。当您按照自己的方式传递函数调用时,您实际上传递了该函数的 return 值。换句话说,就像 Q(3)
将解析为 3
,Q(nodefunc(args))
将解析为任何 nodefunc(args)
returns - 在异步函数的情况下t特别多
我想你想用 ninvoke
(a.k.a. nsend
) 代替。
Q.ninvoke(object, methodName, ...args)
Alias: 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).
猫鼬 API 文档说 connect
是一种同步方法,return 什么都不是。它的近亲 createConnection
将 return 一个连接对象,可能更适合用例。
以下代码同时具有 - 调用 Q
用于 return 实际值的同步方法,以及调用 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);
});
}
(未经测试的代码)
猫鼬 4.0.3
节点 0.10.22
mongod 数据库版本 3.0.1
我正在尝试使用 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);
});
}
这个returnsdata2 is undefined
我试着按照这个问题的答案:Mongoose.js: remove collection or DB
我认为你误解了 mongoose、db 和 Q 的交互方式。
你可以试试(未测试)
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;
});
}
需要使用 exec()
调用 mongoose 函数才能 return Promise。
据我所知,这不会扩展到 dropCollection 方法,因此您需要对其进行去节点化。
我不认为你可以这样使用 Q。
Q()
将您传递给它的任何内容都变成一个承诺。当您按照自己的方式传递函数调用时,您实际上传递了该函数的 return 值。换句话说,就像 Q(3)
将解析为 3
,Q(nodefunc(args))
将解析为任何 nodefunc(args)
returns - 在异步函数的情况下t特别多
我想你想用 ninvoke
(a.k.a. nsend
) 代替。
Q.ninvoke(object, methodName, ...args)
Alias:
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).
猫鼬 API 文档说 connect
是一种同步方法,return 什么都不是。它的近亲 createConnection
将 return 一个连接对象,可能更适合用例。
以下代码同时具有 - 调用 Q
用于 return 实际值的同步方法,以及调用 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);
});
}
(未经测试的代码)