2017-08-14 43 views
0

我已經編寫了一個基於用戶可以製作的兩個搜索詞獲取結果的小函數。條款是:文件夾的owner & path在地圖功能中檢索單獨文檔的問題

下面的地圖功能向下工作到目前爲止,當用戶查找bob & holiday時,它會返回正確的文件夾。 不幸的是,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" 
       ] 
      } 
     } 
    ] 
} 

回答

0

不幸的是,如果elfe部分不工作,我想要檢索的文件的文件。如果我將兩者分開,如果一切正常。爲什麼不組合? :-)

這不起作用,因爲JavaScript沒有elseif關鍵字。使用else if等同於:

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

見:MDN if...else