如何解决此“ TypeError:read()缺少1个必需的位置参数:'self'”

时间:2019-02-13 12:08:18

标签: python-3.x rest flask

我正在使用Flask创建REST api,以获取存储在MSSQL数据库中的所有书籍。当我简单地运行此代码并在控制台中打印数据时,效果很好。但是当我以Flask API运行并尝试在邮递员中获取所有数据时,它显示此错误:

完整追溯:

Traceback (most recent call last):
     File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 2309, in __call__
        return self.wsgi_app(environ, start_response)
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 2295, in wsgi_app
        response = self.handle_exception(e)
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 1741, in handle_exception
        reraise(exc_type, exc_value, tb)
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\_compat.py", line 35, in reraise
        raise value
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 2292, in wsgi_app
        response = self.full_dispatch_request()
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
        rv = self.handle_user_exception(e)
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
        reraise(exc_type, exc_value, tb)
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\_compat.py", line 35, in reraise
        raise value
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
        rv = self.dispatch_request()
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 1799, in dispatch_request
        return self.view_functions[rule.endpoint](**req.view_args)
    TypeError: read() missing 1 required positional argument: 'self'
    127.0.0.1 - - [13/Feb/2019 17:38:21] "GET / HTTP/1.1" 500 -
    Traceback (most recent call last):
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 2309, in __call__
        return self.wsgi_app(environ, start_response)
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 2295, in wsgi_app
        response = self.handle_exception(e)
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 1741, in handle_exception
        reraise(exc_type, exc_value, tb)
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\_compat.py", line 35, in reraise
        raise value
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 2292, in wsgi_app
        response = self.full_dispatch_request()
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
        rv = self.handle_user_exception(e)
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
        reraise(exc_type, exc_value, tb)
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\_compat.py", line 35, in reraise
        raise value
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
        rv = self.dispatch_request()
      File "C:\Users\raj.pandey\Desktop\Flask_API\lib\site-packages\flask\app.py", line 1799, in dispatch_request
        return self.view_functions[rule.endpoint](**req.view_args)
    TypeError: home() missing 1 required positional argument: 'self'

这是我的代码出错:

class MssqlConnection:
    def __init__(self):
        credentials = Credentials()
        self.driver = credentials.driver
        self.host = credentials.host
        self.database = credentials.database
        self.table = credentials.table
        self.user = credentials.user
        self.password = credentials.password

    @app.route('/books')
    def read(self):
        query = "Select id, name, price, isbn from dbo." + self.table
        cursor.execute(query)
        for row in cursor:
            data = {
                'id': row.id,
                'name': row.name,
                'price': row.price,
                'isbn': row.isbn
            }
            # print(data)
            return jsonify(data)

我已经检查过了,但是还是不明白是什么问题。

1 个答案:

答案 0 :(得分:1)

您的端点函数可能不在(甚至不应该)在类中。只需从self行中删除def read(self):参数,就可以了。在类范围内进行编码时,应使用“ self”作为参数。您不在课程范围内。因此解决方案是def read():

在Flask中,仅当将app.route('/books/<somevariable>')之类的参数放入url中时,您才应将somevariable作为参数传递给端点函数并写入def myfunction(somevariable):

您可以阅读有关以下内容的文档: http://flask.pocoo.org/docs/1.0/quickstart/#variable-rules