也许这是一个绝对愚蠢的问题,但我有一个看起来像这样的Pouchdb数据库:
var fragDB = {
_id: new Date().toISOString(),
question: quest,
right: right,
wrong1: wrong1,
wrong2: wrong2,
wrong3: wrong3,
source1: source1,
source2: source2,
tags: tagarr
}
db.put(fragDB);
现在我想从最后一个文档中检索标记作为数组。我试过这样的东西,但这显然不起作用。
var alltags = function(){
db.allDocs({include_docs: true, limit: 1, descending: true}, function(err, response){
if (err) {
console.log(err);
console.log("loading Standard Tags");
return ["..."];
}
if (response){
console.log(response.tags);
return response.tags;
}
});
};
我错过了什么?
答案 0 :(得分:0)
不是一个愚蠢的问题 - 它很难理解,因为PouchDB是异步的:http://pouchdb.com/guides/async-code.html
在你的代码中,你在一个函数中有一个函数,它是子函数,它返回response.tags
,而不是父函数。
我建议您阅读上面的链接,这样您就可以学习如何编写有希望的异步代码,例如:
db.allDocs({
include_docs: true,
limit: 1,
descending: true
}).then(function (response) {
console.log(response.tags);
return response.tags;
}).catch(function (err) {
console.log(err);
console.log("loading Standard Tags");
return ["..."];
}).then(function (tags) {
// do something with the tags
});