CBlite 404 loading View


#1

Hi Guys !

I jave a 404 error when I try to load a view from my design doc., My design document is creating ok and also when i get my design document there is no problem but when I try to get the view inside the design document is where i get a 404 error.

Here is my code

To generate the design document i do this:

createDesignDocument: function(designDocumentName, designDocumentViews) {
    var data = {
        views: designDocumentViews
    }
    return this.makeRequest("PUT", this.dbUrl + this.dbName + "/" + designDocumentName, {}, data);
}

here is how i am trying to get the view

getView: function(designDocumentId, viewId) {
return this.makeRequest(“GET”, this.dbUrl + this.dbName + “/” +designDocumentId+"/_view/"+viewId);
},

makeRequest: function(method, url, params, data) {
    
    var settings = {
        method: method,
        url: url,
        async: false
    }
    if(params) {
        settings.params = params;
    }
    if(data) {
        settings.data = JSON.stringify(data);
    }
    
    settings.success = function(result){
        alert("ok: " + JSON.stringify(result));
        return result;
    }
    settings.error = function(error){
        alert("error: "+ JSON.stringify(error));
        return error;
    }

    var ajax = $.ajax(settings);

	return JSON.parse(ajax.responseText);
}

///// This is my design doc with the view

var designDocView = {“language” : “javascript”,“views”: {“DVProductInventory”: {“map”: “function(doc){if(doc.id){emit(doc.id,null);}}”}}};

/// here i call the method to create it with the name “views2”

cb.createDesignDocument("views2", designDocView);

// here I call the view

cb.getView("views2", "DVProductInventory");	

Please if you know why I receive a 404 error tell me why !

Thanks !
Bests.


#2

It looks like you’re using https://github.com/couchbaselabs/ng-couchbase-lite

The design document should be a dictionary of the views you want to create where the key is the view name and the value is the dictionary containing the map function and possibly a reduce as well.

 {
    "DVProductInventory": {
         "map": "function(doc){if(doc.id){emit(doc.id,null);}}"
    }
 }

When creating the design document, the key should contain _design like so:

cb.createDesignDocument("_design/views2", designDocView);

And similarly to query it:

cb.getView("_design/views2", "DVProductInventory");

I think it’d be more straightforward to remove the requirement for _design in the first parameter, I opened a github issue https://github.com/couchbaselabs/ng-couchbase-lite/issues/3
More examples can be found here https://github.com/couchbaselabs/TodoLite-Ionic/blob/master/www/js/app.js

James


#3

It looks like ng-couchbase-lite’s createDesignDocument is reformatting the JSON, or at least wrapping "views":{ … } around it, before saving it, which might be part of the confusion here. But @rickhdz showed the source of the createDesignDocument function, which doesn’t do that, so I don’t know if he’s using ng-couchbase-lite after all.

I’d have an easier time troubleshooting this if I could see the actual HTTP requests being sent to CBL, instead of trying to interpret the JS functions in my head. Is there a logging mode that can dump the requests in raw form?