【发布时间】:2014-10-31 03:47:00
【问题描述】:
我创建了一个 angularJS 工厂,用于从 IndexedDB 插入和获取一些值。我可以成功地将值插入到 IndexedDB。但是当我尝试从数据库中获取值并将其传递给控制器时,我遇到了问题。
factory.getAllProgressData = function() {
var dbOptions = {};
dbOptions.include_docs = true;
var output = {};
var result = {};
pouchDb.allDocs(dbOptions, function(err, res) {
if (err)
console.log(err);
if(res) {
output.weight = getWeightValues(res.rows);
console.log(output); // <== This console line prints the object
}
});
console.log(output); // <== This console line does NOT print the object
return output;
};
var getWeightValues = function(rows) {
var weightValues = [];
for (var i = 0; i < rows.length; i++) {
weightValues.push(rows[i].doc.Weight);
};
return weightValues;
};
正如您在代码的 cmets 中看到的,当我将对象打印到控制台时,它首先打印。但是当我尝试在下面这样做时,它不会打印。而且返回值也是空的。我对 javascript 很陌生,这种行为对我来说并不清楚。我希望将结果对象返回到Controller。请帮忙。提前致谢。
【问题讨论】:
-
this 有帮助吗?
标签: javascript angularjs promise indexeddb pouchdb