Error running a query on iOS
Hi,
I'm evaluating Couchbase Mobile for iOS for use in an upcoming product. I managed to get creates tested and now I'm trying to test reads/queries and having difficulties.
I create a database that has two documents that each have 10 rows of the form "x", "current date" for the key-value pair, where x is a number from one to 10(basically the connection number as I get the date from a server) and current date, is, well, the current date.
When I try to query, I keep getting:
2012-10-08 10:51:31.396 CouchBaseTest[1660:f803] WARNING: Couldn't parse rows from CouchDB view response
2012-10-08 10:51:31.398 CouchBaseTest[1660:f803] REST: Error = The operation couldn’t be completed. (CouchHTTPError error 500.)
I tossed the URL from my CouchQuery operation into a browser and got a valid JSON response:
http://127.0.0.1:49882/test5k/NetworkTest?endkey_docid=NetworkTest&endke...
However, turning on http fox in FIrefox, I Noticed I was getting a COntent-Length of 0 in the response headers. Could that be throwing off the sdk? I'm using CouchDB/2.0.0-beta (Erlang OTP/R14A) for a server also per the response header. My http status is 304(not modified).
Any ideas? Should I post my code?
Thanks,
Ahmad
Hi Jens,
Thanks for the clarification. I had seen references to both and questions about both in Google Groups and various areas but couldn't really find anything definitive to tell them apart.
I got TouchDB and started going through the guide located here: https://github.com/couchbaselabs/CouchCocoa/wiki/Example-Snippets
but I hit a snag under the Querying the view section. Here's my code snippet:
CouchDesignDocument* design = [delegate.database designDocumentWithName:r.key];
[design defineViewNamed: @"byDate" map: ^(NSDictionary* doc, void (^emit)(id key, id value)) {
id date = [doc objectForKey: @"created_at"];
if (date) emit(date, doc);
} ];
CouchDesignDocument* design2 = [delegate.database designDocumentWithName: r.key];
CouchQuery* query = [design queryViewNamed: @"byDate"];
Where r.key is a string. When I hit the last line in the debugger, the app explodes with:
2012-10-09 09:16:10.586 TouchDB[7182:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__NSGlobalBlock__)'
libc++abi.dylib: terminate called throwing an exception
I got the CouchCocoa source code and found the source for queryViewNamed and pasted it in instead of the function call, and it seems to die on the call to saveChanges.
Does this mean I have malformed data some place? How can I tell where/what it is?
Thanks.
** Edit: I did figure out that this is dying in saveChanges because it is trying to save the code block as a JSON object. When I enclosed the block in quotes and made it an NSString, it got further. But now when I run the query, I get a 500 error:
2012-10-09 09:53:01.083 TouchDB[44969:11303] REST: >> GET touchdb:///my-database/_design/1234/_view/byDate?update_seq=true
09:53:01.588| WARNING*** : View 1234/byDate has unknown map function: ^(NSDictionary* doc, void (^emit)(id key, id value)) {id date = [doc objectForKey: @"created_at"];if (date) emit(date, doc);}
2012-10-09 09:53:01.590 TouchDB[44969:11303] REST: << 500 for GET touchdb:///my-database/_design/1234/_view/byDate?update_seq=true (40 bytes)
2012-10-09 09:53:01.590 TouchDB[44969:11303] REST: Content-Type: application/json
2012-10-09 09:53:01.591 TouchDB[44969:11303] REST: Server: TouchDB 0.961
2012-10-09 09:53:01.592 TouchDB[44969:11303] WARNING: CouchQuery[_view/byDate] failed with Error Domain=CouchDB Code=500 "The operation couldn’t be completed. Database error!" UserInfo=0x7673d70 {NSUnderlyingError=0x76879a0 "internal server error", NSLocalizedFailureReason=Database error!}
2012-10-09 09:53:01.593 TouchDB[44969:11303] REST: Error = The operation couldn’t be completed. Database error!
It seems something is still wrong with my mapping function?
[FYI: Could you subscribe to the "mobile-couchbase" Google group and post there? That's where the discussion is happening. If I had my way we wouldn't have these web forums; they're awkward.]
So based on this:
09:53:01.588| WARNING*** : View 1234/byDate has unknown map function: ^(NSDictionary* doc, void (^emit)(id key, id value)) {id date = [doc objectForKey: @"created_at"];if (date) emit(date, doc);}
This means you haven't defined a map block for the view named "byDate" in the design document named "1234". The snippet you gave above using CouchDesignDocument does show you're registering a map block ... are you sure you're registering it _before_ querying the view? Generally you'd register map blocks at launch time.
Also:
> I did figure out that this is dying in saveChanges because it is trying to save the code block as a JSON object. When I enclosed the block in quotes and made it an NSString, it got further.
I don't know what that means — what code block? And why are you putting code into a document? TouchDB defines map/reduce functions as native code and they don't live in a design document because function pointers can't be persisted as data.
First off, you should really be using TouchDB instead of Couchbase Mobile. It's much smaller and starts faster and is under active development. The Objective-C API is 99% compatible apart from the few lines of code to initialize the server.
http:/touchdb.org
If you get the same error when using TouchDB, we can take a look at troubleshooting it. But we're not supporting Couchbase Mobile for new development.