[英]PouchDb data not updating on Get request
嗨,我已经尝试从pouch db本地数据库中获取数据。
这是我的代码。
function test(){
var jsondata;
var deviceId = localStorage.getItem("deviceId");
localDB.get(deviceId).then(function(doc){
jsondata = doc.data;
});
return jsondata}
test();
在这里,我可以在get函数中获取控制台数据,但是从最后一行开始,我是未定义的。
函数内的部分在承诺完成后执行。
最后的console.log
在localDB.get
语句之后立即执行。 至此,promise尚未解决,回调尚未执行,因此尚未设置jsondata
的值- undefined
为jsondata
的值
编辑1:
var jsondata;
var deviceId = localStorage.getItem("deviceId");
localDB.get(deviceId).then(function(doc){
jsondata = doc.data;
console.log(jsondata)
// whatever you want to do with `jsondata`,
// do it here
});
// not here
编辑2:
如果将此代码放置在函数中,则应返回promise本身。 因此,您的代码变为:
function getDoc(){
var deviceId = localStorage.getItem("deviceId");
return localDB.get(deviceId);
}
现在.then()
在您需要使用jsondata
代码中的那个位置使用jsondata
.then()
解决此承诺
.
.
getDoc().then(function(doc){
// your code that needs to use the document
});
.
.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.