在 Map-Function 中检索单独的文档时出现问题

Problems retrieving separate Document in Map-Function

我编写了一个小函数来根据用户可以使用的两个搜索词获取结果。条款是:owner & path 一个文件夹。

下面的地图功能目前有效,当用户查找 bobholiday 时,它会返回正确的文件夹。 不幸的是 elfe if 部分不起作用,我想在其中检索文件文档。如果我将两个 if 分开,一切正常。为什么不组合? :-)

地图函数:

function(doc) {
    if (doc.type == 'folder') {
      emit([doc.owner, doc.path], null);
    } else if (doc.files) {    
    for (var i in doc.files) {
          emit(doc.owner, {_id: doc.files[i]});
      } 
  }
}

文件:

// Folder
{
  "_id": "folder.abc",
  "name": "Holiday",
  "type": "folder",
  "owner": "bob",
  "path": "\holiday",
  "files": [
    "file.123"
    ]
}

// Files
{
  "_id": "file.123",
  "name": "Hawaii.jpg",
  "type": "file",
}

搜索查询:

[Snip]?key=["bob","\holiday"]&include_docs=true

结果:

{
    "total_rows":4,
    "offset":2,
    "rows":[
        {
            "id":"folder.abc",
            "key":[
                "bob",
                "\holiday"
            ],
            "value":null,
            "doc":{
                "_id":"folder.abc",
                "name":"Holiday",
                "type":"folder",
                "owner":"bob",
                "path":"\holiday",
                "files":[
                    "file.123"
                ]
            }
        }
    ]
}

Unfortunately the elfe if part doesn't work, where I want to retrieve the files document. If I separate both if's everything works. Why not in combination? :-)

这不起作用,因为 JavaScript 没有 elseif 关键字。使用 else if 等同于:

if (doc.type === 'folder') {
    ...
} else {
    if (doc.files) {
        ...
    }
}

参见:MDN if...else