简体   繁体   中英

Problems retrieving separate Document in Map-Function

I've written a little function to get results based on two search terms a user can make. The terms are: owner & path of a folder.

The map function down below works so far that it gives me back the right folder when a user looks for bob & holiday . 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? :-)

Map-Function:

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]});
      } 
  }
}

Documents:

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

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

Search Query:

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

Result:

{
    "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? :-)

This doesn't work because JavaScript doesn't have an elseif keyword. Using else if is equivalent to:

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

see: MDN if...else

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM