Return list of JSONs

1

In path /getAll I want to return a list of JSONs however I get an object is not callable , why does this occur and how can I return to my JSON list?

  • MyList was called list and after reading the following definition of callable the error persisted.

      

    TypeError: 'str' object is not callable

         

    TypeError: 'list' object is not callable

         

    These can occur when attempting to assign values to variables   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.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
    
        
  • asked by anonymous 27.03.2016 / 21:57

    1 answer

    2

    Daniela, you need to see how myList is returning, to return a json you just need to use the "return jsonify (myList)".

    According to my tests this code snippet is only returning the keys of your dict:

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

    See how myList is returning, and if you can, send it here.

        
    28.03.2016 / 00:10