1

On couchDB I have the following view:

function(doc) { if(doc._id === 'countryMobileNumberCodes') { Object.keys(doc.dictionary).forEach(function(k, i) { emit(k, doc.dictionary[k]); }); } } 

This emits the following result:

{"total_rows":2,"offset":0,"rows":[ {"id":"countryMobileNumberCodes","key":"1","value":"+265"}, {"id":"countryMobileNumberCodes","key":"2","value":"+27"} ]} 

This same view on Android (using couchbase-lite-android 1.2.1) does not emit any rows. However, if I were to change the view to this:

function(doc) { if(doc._id === 'countryMobileNumberCodes') { emit(doc._id, doc.dictionary); } } 

Then I do get results from couchbase-lite (so the docs definitely exist on the mobile device).

Does CouchDB implement MapReduce differently to couchbase-lite?

I have seen reference to the fact that couchbase-lite and CouchDB are 100% compatible, but that does not seem to be the case (Couchbase-lite and CouchDB).

1
  • They are compatible in that they can sync with each other, but other than that they are two products developed by two entities. They use different Javascript engines, etc. Commented May 24, 2016 at 21:12

1 Answer 1

0

Presumably you are using Couchbase Lite's REST API, probably within PhoneGap or Cordova. It looks as though the JavaScript interpreter in that container doesn't support Object.keys or foreach(). You'll probably need to rewrite your map and reduce functions using only older features of JavaScript.

(My JS is very rusty so I can't tell you what version of the language added those APIs. It's also possible they're not built-in but are part of a library that CouchDB includes in its JS context as a convenience.)

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer. A for (var key in obj) loop also didn't work
Well, all I can say is that the map/reduce implementation is the same as CouchDB, so whatever you're running into is something that has to do with JS programming, that's preventing the emit function from being called. You'll need to find a way to debug the contents of the map function so you can see what it's doing wrong. Maybe copy the code somewhere else in your app where you can debug it more easily?
Couchbase-lite is running on Android. There is a result from the view, the rows arrays is empty. Is it still possible that emit is not being called?
If there aren't any rows in the query result, and you're not constraining the query with a startkey/endkey/keys param, then emit isn't being called, yes. Every call to emit adds a row to the view index.
Thanks. I had never considered that no results in the rows array means that the emit() function is never called. Seems obvious in hindsight. However I still can't see a reason that for (var k in doc.obj) {if doc.obj.hasOwnProperty(k) {...}} wouldn't work. the code works on CouchDB. And the documents definitely exist in Couchbase-lite.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.