Return list of Jsons

Asked

Viewed 448 times

2

On the path /getAll wish to return a list of Jsons but get one 'list' Object is not callable, why this occurs and how can I return to my JSON list?
Formerly, Mylist was called list and after reading the following callable definition the error persisted.

Typeerror: 'str' Object is not callable

Typeerror: 'list' Object is not callable

These can occur when attempting to assign a value to variables with Hese Names, or by mutating their values.

Code

from flask import Flask, render_template, Response
import pymongo
from bson.json_util import dumps
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/getAll', methods=['GET'])
def getAll():
    try:
        conn = pymongo.MongoClient()
    except pymongo.errors.ConnectionFailure, e:
        print "Could not connect to MongoDB: %s" % e
    db = conn['local']
    collection = db.teste
    myList = []
    for d in collection.find():
        myList.append(dumps(d))
    return myList


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=True)

Tracktrace

127.0.0.1 - - [27/Mar/2016 16:51:44] "GET /getAll HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1478, in full_dispatch_request
    response = self.make_response(rv)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1577, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File "/usr/lib/python2.7/site-packages/werkzeug/wrappers.py", line 847, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File "/usr/lib/python2.7/site-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app
    return _run_wsgi_app(*args)
  File "/usr/lib/python2.7/site-packages/werkzeug/test.py", line 871, in run_wsgi_app
    app_rv = app(environ, start_response)
TypeError: 'list' object is not callable

1 answer

2

Daniela, you need to see how this Mylist is returning, to return a json you would only need to use "Return jsonify(Mylist)".

According to my tests this snippet of code is only returning the Keys from your Dict:

for d in {'a':1, 'b':2}:
    myList.append(dumps(d))
    # o retorna será ['a', 'b']

See how this myLista is returning, and if you can rule here.

  • Just to add: the result of your method has to be a string.

  • But she wants to return a json, no?

  • @victorhos From what they explained to me, the routes only accept string as return. "Resolvi" returning the list of JSON in string format.

  • @Danielamorais tendi, so it’s solved né hehe, but then take a look at this piece of documentation, it might help: > Creates a Response with the JSON representation of the Given Arguments > with an application/json mimetype. The Arguments to this Function are the > same as to the Dict constructor. <http://flask.pocoo.org/docs/0.10/api/#module-flask.json>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.