从 PouchDB 查询返回数据 [英] Returning data from a PouchDB query

查看:55
本文介绍了从 PouchDB 查询返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从 PouchDB 查询返回数据时遇到问题.我正在尝试构建一个函数,该函数在调用时从 PouchDB 返回特定数据.这是我的代码:

I'm having a problem returning data from a PouchDB query. I'm trying to build a function that, when called, returns specific data from a PouchDB. Here is my code:

function getUserFullName() {
            var db = "userInfo";
            var pouch = new PouchDB(db);
            pouch.get("token").then(function (result) {
            console.log(result-user_real_name);
                return result.user_real_name;
            }).catch(function (error) {
                console.log(error);
            });
        }

所以会发生什么是函数返回一个未定义的.有没有人知道我做错了什么?

So what happens is that the function returns an undefined. Does anyone have a clue as to what I'm doing wrong?

推荐答案

问题是看起来您正在同步运行getUserFullName",但其中包含一个异步函数pouch.get".该异步函数的返回值需要在回调或承诺中返回.

The issue is that it looks likes you are running "getUserFullName" synchronously, but you have an asynchronous function inside of it, "pouch.get". The return value of that asynchronous function needs to be returned in a callback or a promise.

如果pouch.get"返回一个promise,就像上面用.then"显示的那样,你可以像这样编写代码:

If "pouch.get" returns a promise, as you are showing above with the ".then" you can write your code like this:

function getUserFullName() {
  var db = "userInfo";
  var pouch = new PouchDB(db);

  return pouch.get("token")
}

然后像这样运行:

getUserFullName()
  .then(function(fullUserName){
    console.log(fullUserName);
  })
  .catch(function(err){
    console.log(err);
  });

告诉我这是否有效,或者您有任何问题.谢谢!

Let me know if that works, or if you have any questions. Thanks!

看起来pouch.get"确实返回了一个承诺.在他们的此处的文档中查看示例.因此,此代码将起作用.

Looks like "pouch.get" does return a promise. See an example in their docs here. Therefore, this code will work.

这篇关于从 PouchDB 查询返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆