Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Unable to access views on CouchDB #449

Closed
Lars opened this issue Aug 5, 2019 · 2 comments
Closed

Unable to access views on CouchDB #449

Lars opened this issue Aug 5, 2019 · 2 comments

Comments

@Lars
Copy link

Lars commented Aug 5, 2019

I'm doing some dockerized code in Python (3.5) and flask (1.1.1) working against a CouchDB database (2.3.1) using the cloudant python extension (2.12.0).

I'm trying to fetch and use a view from the database, but it is not working. I can fetch documents, and work with the database normally, but I can't use the view.

I've added a print statement for the object that should hold the design document at the program start, and I see that the document shows as having no views (or anything at all) AND the CouchDB log shows NO requests for the design document being made.

I also tried to both get the design document and use the view via curl using the same URL and username/password, and both actions work successfully.

Here's sample code that fails:

from flask import Flask, render_template , request, g
from cloudant.client import CouchDB
from cloudant.view import View
from cloudant.design_document import DesignDocument
import requests

application = Flask(__name__)
application.config.from_pyfile("config.py")

couch = CouchDB(application.config['COUCHDB_USER'], application.config['COUCHDB_PASSWORD'], url=application.config['COUCHDB_SERVER'], connect=True, auto_renew=True)
database = couch[application.config['COUCHDB_DATABASE']]
views = DesignDocument(database, '_design/vistas')
print(views)
print(views.list_views())

@application.route("/", methods=['GET', 'POST'])
def index():
        for pelicula in View(views,'titulos_peliculas'):
            titulos.append({ "id": pelicula['id'], "titulo": pelicula['key'] })
        return render_template('menu.html',titulos=titulos)

In that code, the print of the design document (views) returns:

{'lists': {}, 'indexes': {}, 'views': {}, 'shows': {}, '_id': '_design/vistas'}

With empty views as show... And the CouchDB log only shows the login to the database and getting the DB info:

couchdb:5984 172.23.0.4 undefined POST /_session 200 ok 69
couchdb:5984 172.23.0.4 vmb_web HEAD //peliculas 200 ok 232

No other queries at all.

No errors in the app log either. Even when I call the routed use of the views:

[pid: 21|app: 0|req: 1/1] 172.23.0.1 () {52 vars in 1225 bytes} [Mon Aug  5 15:03:24 2019] POST / => generated 1148 bytes in 56 msecs (HTTP/1.1 200) 2 headers in 81 bytes (1 switches on core 0)

And, as I said, I can get, and use the document:

curl http://vmb_web:[email protected]:999/peliculas/_design/vistas
{"_id":"_design/vistas","_rev":"1-e8108d41a6627ea61b9a89a637f574eb","language":"javascript","views":{"peliculas":{"map":"function(doc) {         if (doc.schema == 'pelicula') {            emit(doc.titulo, null);            for(i=0;i<doc.titulos_alt.length;i++) {                emit(doc.titulos_alt[i],null);            }            for(i=0;i<doc.directores.length;i++) {                emit(doc.directores[i].nombre,null);            }            for(i=0;i<doc.actores.length;i++) {                emit(doc.actores[i].nombre,null);            }            for(i=0;i<doc.escritores.length;i++) {                emit(doc.escritores[i].nombre,null);            }            for(i=0;i<doc.etiquetas.length;i++) {                emit(doc.etiquetas[i],null);            }        }        }"},"titulos_peliculas":{"map":"function(doc) {         if ((doc.schema == 'pelicula') && (doc.titulo)) {            emit(doc.titulo, null);        }        }"},"archivos_peliculas":{"map":"function(doc) {         if ((doc.schema == 'pelicula') && (doc.titulo)) {            emit(doc.titulo, doc.archivo);        }        }"},"titulo_rev":{"map":"function(doc) {         if ((doc.schema == 'pelicula') && (doc.titulo)) {            emit(doc.titulo, doc._rev);        }        }"}}}
@emlaver
Copy link
Member

emlaver commented Aug 5, 2019

Hello @Lars, you'll need to first fetch the design document from the server:

ddoc_with_view = DesignDocument(database, '_design/vistas')
ddoc_with_view.fetch()

You can also use the recommended database helper method get_view_result.

@Lars
Copy link
Author

Lars commented Aug 5, 2019

Thank you for your help, it is indeed, as you say.

@Lars Lars closed this as completed Aug 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants