Question

I'm using Node.js with Cradle to call a CouchDB update handler. I need to pass an array in the querystring, but when I read the req.query object in CouchDB, only the first array value is available. So, for example using Cradle .update method:

db.update("myview/myupdate", id, {title:'sometitle',tags:['one','two']}, function }...

But when I check the QS value passed to CouchDB it's:

?title=sometitle&tags=one&tags=two

Then in the CouchDB update function, req.query.tags only gives me "two". I noticed that Cradle uses require("querystring") to do the "querystring.stringify", so I also tried visionmedia's require("qs") but then "qs.stringify" gives me:

?title=sometitle&tags[]=one&tags[]=two

It would seem that CouchDB wants the tags array passed as:

?title=sometitle&tags=["one","two"]

How can I build a querystring that looks like that?

Was it helpful?

Solution

You may use Connect and his query middleware.

npm install connect
var connect = require('connect');

var server = connect();
server.use(connect.query());
server.use(function (req, res, next) {
    res.end(JSON.stringify(req.query));
});
server.listen(3001);

Point your browser to http://127.0.0.1:3001/?tags=tag1&tags=tag2&tags=tag3

You will see: {"tags":["tag1","tag2","tag3"]}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top