Search code examples
node.jscouchdbcradle

Order results in Cradle


How can I order results from a couchdb database, using cradle for node.js? From the docs it seems that adding a descending=true parameter to my url should work. My current code is as follows:

this.db.view('articles/all',function(error, result) {

    if( error ){
        callback(error)
    }else{
        var docs = [];
        result.forEach(function (row){
            docs.push(row);
        });
        callback(null, docs);
    }

});

The articles are ordered by date (as set in my view below)

function (doc) { 
    if (doc.created_at) emit(doc.created_at, doc);
}

If I change the first line to

this.db.view('articles/all?descending=true',function(error, result) {

no results are returned

So, how can I order results in cradle for couchdb?

Thanks!


Solution

  • That is the correct CouchDB parameter. From looking at the cradle code, I don't think you pass the parameter as part of the query path (the first argument). I think cradle is taking your query string and messing it up. Maybe you could check the what actual request string is being generated? It looks like the function may take another argument which are the options. So maybe it is something like:

    this.db.view('articles/all',{descending: true},function(error, result) {
    

    Hope that gets you closer. Sorry, I haven't used that library and my JavaScript isn't exactly awesome.

    I didn't see the comment by Marcello Nuccio your question. I guess he pointed to the answer first.