【问题标题】:CouchApp: List function with include_docs=trueCouchApp:包含 include_docs=true 的列表函数
【发布时间】:2012-07-25 12:01:30
【问题描述】:

我正在编写一个简单的 CMS CouchApp 作为宠物项目。我在 OS X 10.7.4 上运行 CouchDB 1.2.0 并使用 Python CouchApp 开发工具。

基本上,我希望将页面作为文档,并且页面将引用另一个包含站点共享布局的文档。

页面文档示例:

{
   "_id": "index",
   "_rev": "3-d5451ea54212ae6ec0d8d2d95c5f225d",
   "content": "<img src=\"/images/img.jpg\"/> <p>Lorem ipsum dolor sit amet.</p>",
   "layout": "layouts/default"
}

布局文档示例:

{
   "_id": "layouts/default",
   "_rev": "1-d2fa96e15ab8768828b262d81265f3d2",
   "content": "<!DOCTYPE html> <html><head> <title>Foo</title> </head><body><div>{{content}}</div></body> </html>"
}

所以基本上要呈现一个页面,我需要获取两个文档。然后我会使用 Mustache 将页面呈现到布局中。

我在 show 函数上摆弄了一阵子,但找不到在函数中获取布局文档的方法。然后我偶然发现了include_docs,我现在正在尝试使用列表函数和视图来让它工作。我的地图功能如下:

function(doc) {
    if (doc.layout) {
        emit([doc._id, 1], doc);
        emit([doc._id, 0], {_id: doc.layout });
    }
};

当我在提供参数include_docs=true&amp;startkey=["index",0]&amp;endkey=["index",1] 的浏览器中导航到视图本身时,它工作得很好并且正在加载布局文档。

但是,布局文档不会传递给列表函数。在上述视图上运行此列表函数

function(head, req) {  
    var doc = null;
    var row = getRow();

    do
    {   
        if (!row.value.layout){
            doc = row.value;
        }
    } while (row = getRow())

    for (i in doc) {
        send(i);
    }
}

...用相同的参数渲染:

_id

我做了一些谷歌搜索,发现有a bug in CouchDB 链接的文档没有传递到列表函数。据我所知,这应该已经修复了。这是退步还是我智障?

【问题讨论】:

    标签: couchdb couchapp


    【解决方案1】:

    该行的值存储在 row.value 中,但该行的文档在 row.doc 中。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 2020-02-22
      • 1970-01-01
      相关资源
      最近更新 更多