couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jeroen van Dijk <jeroentjevand...@gmail.com>
Subject Re: Setting default values on create server side
Date Tue, 19 Apr 2011 09:58:25 GMT
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 <zachary.zolton@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
> <jeroentjevandijk@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
> >
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message