You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Jeffrey Ollie <je...@ocjtech.us> on 2010/03/17 18:31:50 UTC

Problem using Views

Hello,

I am trying to use a view in a project, but I'm not having much luck.
I'm using the following as my map function:

function(doc) {
 if (doc.record_type ==
"http://www.geocaching.com/track/details.aspx" && !(typeof
doc.location === "undefined")) {
   emit(doc.location, null);
 }
}

In my code I do the following:

results = database.execute_view('trackables_by_location', 'geocaching')

and then pretty much any usage of "results" gives me a
"httplib.BadStatusLine" exception because the CouchDB server seems to
be cutting off the HTTP connection without returning any headers or
data.  I've attached the logs from an attempt and it looks like the
error message on the server side is "Cannot encode 'undefined' value
as JSON".  Checking the FAQ for that error message pointed me to map
functions emtting undefined values, but I think I have that taken care
of.  The only other oddness is that there
aren't any records in the database that would be in the view at the
time I'm executing the query.

I'm pretty new to the CouchDB scene so I'm pretty sure I'm doing
something wrong, I just can't figure out *what*.  I'm using
desktopcouch 0.6.3, python-couchdb 0.6.1, couchdb 0.10.1, and erlang
R13B-04 on Fedora 12.

--
Jeff Ollie

Re: Problem using Views

Posted by Jeffrey Ollie <je...@ocjtech.us>.
On Wed, Mar 17, 2010 at 3:18 PM, Brian Candler <B....@pobox.com> wrote:
>
> If you're still getting the 'undefined as JSON' error then it could be
> something tangential like looking at the wrong view. Try using curl -v as
> the client, as that eliminates any uncertainty about your HTTP client.

Uggg... Thanks for making me look at the other views.  Turns out my
problem was with a reduce function on a different view:

function(key, values) {
  sum(values);
}

The function needs to be written as:

function(key, values) {
  return sum(values);
}

I didn't scrutinize the other views because even though I had other
views defined I hadn't written any code to make use of them yet.

-- 
Jeff Ollie

Re: Problem using Views

Posted by Brian Candler <B....@pobox.com>.
On Wed, Mar 17, 2010 at 03:01:27PM -0500, Jeffrey Ollie wrote:
> On Wed, Mar 17, 2010 at 12:31 PM, Jeffrey Ollie <je...@ocjtech.us> wrote:
> >
> > function(doc) {
> >  if (doc.record_type ==
> > "http://www.geocaching.com/track/details.aspx" && !(typeof
> > doc.location === "undefined")) {
> >    emit(doc.location, null);
> >  }
> > }
> 
> Ok, I'm confused...  The above works fine as the map function for a
> temporary view, but fails when it's used as a permanent view.  Even if
> I go into Futon and enter the code into the temporary view, check that
> it runs, and then use the "save as" button to convert it to a
> permanent view I get the "Cannot encode 'undefined' value as JSON"
> error.  What am I missing here?

I don't really know, but try this as a first stab at isolating the problem:

  emit(doc.location || "oops", null);

If you're still getting the 'undefined as JSON' error then it could be
something tangential like looking at the wrong view. Try using curl -v as
the client, as that eliminates any uncertainty about your HTTP client.

Re: Problem using Views

Posted by Jeffrey Ollie <je...@ocjtech.us>.
On Wed, Mar 17, 2010 at 12:31 PM, Jeffrey Ollie <je...@ocjtech.us> wrote:
>
> function(doc) {
>  if (doc.record_type ==
> "http://www.geocaching.com/track/details.aspx" && !(typeof
> doc.location === "undefined")) {
>    emit(doc.location, null);
>  }
> }

Ok, I'm confused...  The above works fine as the map function for a
temporary view, but fails when it's used as a permanent view.  Even if
I go into Futon and enter the code into the temporary view, check that
it runs, and then use the "save as" button to convert it to a
permanent view I get the "Cannot encode 'undefined' value as JSON"
error.  What am I missing here?

-- 
Jeff Ollie