0

In path /getAll I want return a list of JSONs but I get list object is not callable.
Why this happens and how I can return my list of Json? Previously myList was list and after read the definition I changed "list" to "myList" but the error persists.

TypeError: 'str' object is not callable

TypeError: 'list' object is not callable

These can occur when attempting to assign a value to variables with these 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.test
    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
Daniela Morais
  • 2,125
  • 7
  • 28
  • 49
  • Have you tried printing the variables in `for d in collection.find():`? E.g. what is `dumps`? `json.dumps` redefined? – jDo Mar 27 '16 at 21:28
  • Yes, there is no problem with JSON . I found that because of app.route @ I can only return string , so I modified: `return "[" + ",".join(myList) + "]"` – Daniela Morais Mar 27 '16 at 21:35
  • Btw. `TypeError: 'list' object is not callable` is typically the result of trying to use parentheses instead of square brackets for list indexing. I.e. `my_list(0)` is wrong and `my_list[0]` is right. – jDo Mar 27 '16 at 21:53
  • I suspect the `TypeError` is not being invoked by `myList`. Note that your stack trace does not contain any mention of a line within your code. It appears trying to return a raw list by itself causes Flask to break down. (Which makes sense - considering [jsonifying](http://stackoverflow.com/questions/12435297/how-do-i-jsonify-a-list-in-flask) the list you return). – Akshat Mahajan Mar 27 '16 at 22:26

0 Answers0