【发布时间】:2016-12-22 22:59:22
【问题描述】:
您好,我正在编写一个更新 mongodb 数据库的脚本。我的脚本执行以下操作。
- 解析文件结构并为目录中的所有文件准备架构对象。
- 然后查询数据库并获取数据库中的文件文档。
- 然后我使用第 1 步和第 2 步中准备的数组循环查找合适的方法,即保存或更新。
- 断开数据库。
由于 mongodb API 的异步特性,我不得不使用 Promises。但我无法通过承诺来实现这一目标。以下是我的代码。
// a array that stores the file Json objects
var fileJsonArray = [];
//passing the filePath twice as one serves as the source for the icons and the other is the base location.
//Check the documentaion for the file for more details
directoryParser.parseDirectory(...);
//console.log(fileJsonArray);
//connect to db
var mongoose = mongoose || require('mongoose');
mongoose.Promise = require('bluebird');
var dbUrl = 'mongodb://'+ config.dbLocation + ':' + config.dbPort + '/' + config.dbName;
mongoose.connect(dbUrl);
// an array that contains all the promises
var promises = [];
//console.log(fileJsonArray);
// get all the element
promises.push(File.find({}).exec()
.catch(function(err){
console.error('Error while getting all elements',err);
})
.then(function(err, allElements) {
if(err) {
console.error(err);
} else {
console.log(fileJsonArray);
for (var i = 0; i < fileJsonArray.length; i++) {
console.log(fileJsonArray[i].name);
if(fileJsonArray[i] in allElements) {
var query = {name : fileJsonArray[i].name, tags: fileJsonArray[i].tags}
, update = {size : fileJsonArray[i].size, bmarkedasdeleted: true}
, options = {multi: false};
promises.push(File.update(query, update, options).exec());
allElements.splice(allElements.indexOf(fileJsonArray[i]),1);
} else {
promises.push(fileJsonArray[i].save(function(err) {
if(err) {
console.error(fileJsonArray, err);
}
}));
}
}
for (var i = 0; i < allElements.length; i++) {
var query = {name : allElements[i].name, tags: allElements[i].tags}
, update = {bmarkedasdeleted: false}
, options = {multi: false};
promises.push(File.update(query, update, options).exec());
}
}
}));
//console.log(promises);
Promise.all(promises).then(function() {
mongoose.disconnect();
console.log('Disconnect Mongoose.');
}).catch(function(err){
console.log(err);
});
现在我无法保存任何值,因为 find({}).exec() 中的 fileJsonArray 为空。
请指教。如果需要任何说明,也可以添加一些 cmets。
【问题讨论】:
-
只是不明白你为什么使用
promises.push(File.find({}).exec()。您应该使用 File.find({}).exec() 获取所有元素,然后使用 promise.all 并发执行。 -
我这样做是因为它可以帮助我收集数组中的所有承诺,然后我可以使用该数组调用 promises.all
标签: javascript node.js mongodb mongoose promise