【问题标题】:Get documents using find() method in couchdb-nano在 couchdb-nano 中使用 find() 方法获取文档
【发布时间】:2019-05-23 14:26:38
【问题描述】:

由于 CouchDB 没有任何集合,我向我的实体添加了自定义 typeproperty。现在我想过滤该属性上的所有实体,例如通过{type:'user'} 获取所有用户。在 couchdb-doc 中,我找到了 method called 'find()',它也在 nano 类型中实现,但在 couchdb-nano 中缺少文档。根据定义,我写了如下代码:

class UserModel {
    type: string = 'User';
    name: string = '';
    mail: string = '';
}
let db = <nano.DocumentScope<UserModel>>nano("http://localhost:5984/testdb");
let query: nano.MangoQuery = { selector: { type: "User" } };
db.find(query, (cb:nano.Callback<nano.MangoResponse<UserModel>>) => {
    // How to get the results here? cb is a callback, but this doesn't make sense
});

收到回调对我来说没有意义。我怎样才能得到结果?

尝试使用某种回调:

db.find(query, (users: nano.MangoResponse<UserModel>) => {
    console.log(users);
});

但是 users 是未定义的,尽管过滤器 { selector: { type: "User" } } 在 Fauxton 项目中运行良好。

【问题讨论】:

    标签: node.js typescript couchdb couchdb-nano


    【解决方案1】:

    nano documentation中提到的:

    在 nano 中,回调函数总是接收三个参数:

    1. err - 错误(如果有)。
    2. body - 来自 CouchDB 的 HTTP 响应正文,如果没有错误。 JSON 解析的正文,非 JSON 响应的二进制文件。
    3. header - 来自 CouchDB 的 HTTP 响应标头,如果没有错误。

    因此,对于db.find,您将拥有:

    db.find(query, (err, body, header) => {
        if (err) {
            console.log('Error thrown: ', err.message);
            return;
        }
        console.log('HTTP header received: ', header)
        console.log('HTTP body received: ', body)
    });
    

    没有使用typescript,但我认为您可以使用typescript 做同样的事情。

    【讨论】:

      猜你喜欢
      • 2012-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多