You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Jeroen van Dijk <je...@gmail.com> on 2011/04/08 11:24:27 UTC

Setting default values on create server side

Hi all,

I'm having troubles setting default values on new documents and have
everything work like it would normally work.

I have created a simple couchapp with the following rewrite rule:

{ "from": "api/v1/apps",
  "to" : "../zwapp-relax/_update/callbacks",
  "method": "POST"
 },

This will trigger the callbacks function which should set the created_at
value:

function(doc, req) {
  var newDoc = req.body;
  newDoc._id = req.uuid
  newDoc.created_at = 1;

  return [newDoc,"OK"];
}

Everything seems to work but req.body is not parsed as JSON even though I'm
setting the header 'Content-Type' to 'application/json'. req.body is just a
string. When I remove the _update/callbacks part from the rewrite I
get "reason":"Referer header required."

Any idea of what I'm doing wrong or how I should approach setting default
values server side?

Thanks,

Jeroen

PS. The answer to this question also implies there are some difficulties
doing the above:
http://stackoverflow.com/questions/3009925/couchdb-automatic-timestamps

Re: Setting default values on create server side

Posted by Jeroen van Dijk <je...@gmail.com>.
So I finally got it working, I think I made some stupid error. It works
perfectly with the rewrites api. Here is my final code:

function(doc, req) {

  var newDoc;

  try {
    newDoc = JSON.parse(req.body);
  } catch(exception) {
    newDoc = {};
  }

  newDoc._id = req.uuid;
  newDoc.created_at = new Date();

  // More formatting



  return [newDoc, "ok"];
}

On Fri, Apr 8, 2011 at 7:28 PM, Zachary Zolton <za...@gmail.com>wrote:

> Update handlers have no intelligent decoding of the request body, if
> it's supposed to be JSON then call JSON.parse() on it.
>
> On Fri, Apr 8, 2011 at 4:24 AM, Jeroen van Dijk
> <je...@gmail.com> wrote:
> > Hi all,
> >
> > I'm having troubles setting default values on new documents and have
> > everything work like it would normally work.
> >
> > I have created a simple couchapp with the following rewrite rule:
> >
> > { "from": "api/v1/apps",
> >  "to" : "../zwapp-relax/_update/callbacks",
> >  "method": "POST"
> >  },
> >
> > This will trigger the callbacks function which should set the created_at
> > value:
> >
> > function(doc, req) {
> >  var newDoc = req.body;
> >  newDoc._id = req.uuid
> >  newDoc.created_at = 1;
> >
> >  return [newDoc,"OK"];
> > }
> >
> > Everything seems to work but req.body is not parsed as JSON even though
> I'm
> > setting the header 'Content-Type' to 'application/json'. req.body is just
> a
> > string. When I remove the _update/callbacks part from the rewrite I
> > get "reason":"Referer header required."
> >
> > Any idea of what I'm doing wrong or how I should approach setting default
> > values server side?
> >
> > Thanks,
> >
> > Jeroen
> >
> > PS. The answer to this question also implies there are some difficulties
> > doing the above:
> > http://stackoverflow.com/questions/3009925/couchdb-automatic-timestamps
> >
>

Re: Setting default values on create server side

Posted by Zachary Zolton <za...@gmail.com>.
Update handlers have no intelligent decoding of the request body, if
it's supposed to be JSON then call JSON.parse() on it.

On Fri, Apr 8, 2011 at 4:24 AM, Jeroen van Dijk
<je...@gmail.com> wrote:
> Hi all,
>
> I'm having troubles setting default values on new documents and have
> everything work like it would normally work.
>
> I have created a simple couchapp with the following rewrite rule:
>
> { "from": "api/v1/apps",
>  "to" : "../zwapp-relax/_update/callbacks",
>  "method": "POST"
>  },
>
> This will trigger the callbacks function which should set the created_at
> value:
>
> function(doc, req) {
>  var newDoc = req.body;
>  newDoc._id = req.uuid
>  newDoc.created_at = 1;
>
>  return [newDoc,"OK"];
> }
>
> Everything seems to work but req.body is not parsed as JSON even though I'm
> setting the header 'Content-Type' to 'application/json'. req.body is just a
> string. When I remove the _update/callbacks part from the rewrite I
> get "reason":"Referer header required."
>
> Any idea of what I'm doing wrong or how I should approach setting default
> values server side?
>
> Thanks,
>
> Jeroen
>
> PS. The answer to this question also implies there are some difficulties
> doing the above:
> http://stackoverflow.com/questions/3009925/couchdb-automatic-timestamps
>