当前位置: 首页 > 知识库问答 >
问题:

TypeError:返回类型必须是字符串、dice、tuple、响应实例或WSGI可调用的,但它是一个协程

孟茂学
2023-03-14

我正在使用flask和MongoDB创建和启动AWS EC2实例,并为不同用户记录这些实例。但当我试图调用我编写的用于检索文档的函数时,会返回以下错误:

TypeError:视图函数没有返回有效的响应。返回类型必须是字符串、字典、元组、响应实例或WSGI callable,但它是一个协程。

追溯:

Traceback (most recent call last):
File "C:\Users\vishn\.virtualenvs\GameFlow-backend(flask)-QWbAV1pw\Lib\site-packages\flask\app.py", line 2088, in __call__
    return self.wsgi_app(environ, start_response)
File "C:\Users\vishn\.virtualenvs\GameFlow-backend(flask)-QWbAV1pw\Lib\site-packages\flask\app.py", line 2073, in wsgi_app
    response = self.handle_exception(e)
File "C:\Users\vishn\.virtualenvs\GameFlow-backend(flask)-QWbAV1pw\Lib\site-packages\flask\app.py", line 2070, in wsgi_app
    response = self.full_dispatch_request()
File "C:\Users\vishn\.virtualenvs\GameFlow-backend(flask)-QWbAV1pw\Lib\site-packages\flask\app.py", line 1516, in full_dispatch_request
    return self.finalize_request(rv)
File "C:\Users\vishn\.virtualenvs\GameFlow-backend(flask)-QWbAV1pw\Lib\site-packages\flask\app.py", line 1535, in finalize_request
    response = self.make_response(rv)
File "C:\Users\vishn\.virtualenvs\GameFlow-backend(flask)-QWbAV1pw\Lib\site-packages\flask\app.py", line 1727, in make_response
    raise TypeError( TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple,
Response instance, or WSGI callable, but it was a coroutine.
127.0.0.1 - - [14/Jun/2021 08:37:23] "GET /dashboard?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 200 -
127.0.0.1 - - [14/Jun/2021 08:37:23] "GET /dashboard?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 200 -
127.0.0.1 - - [14/Jun/2021 08:37:23] "GET /dashboard?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -
127.0.0.1 - - [14/Jun/2021 08:37:23] "GET /dashboard?__debugger__=yes&cmd=resource&f=ubuntu.ttf HTTP/1.1" 200 -
127.0.0.1 - - [14/Jun/2021 08:37:23] "GET /dashboard?__debugger__=yes&cmd=resource&f=console.png HTTP/1.1" 200 -

我的数据库代码是:

client = motor.AsyncIOMotorClient(MONGO_DETAILS)

database = client['vm-data']
db = database


async def create_user(data_value: dict):
    new_value = await db["vm-form"].insert_one(data_value)
    created_value = await db["vm-form"].find_one({"_id": new_value.inserted_id})
    print(created_value)
    return created_value


async def retrieve_data(data_id):
    data_val = await db['vm-form'].find_one({'email': data_id})
    return data_val


async def update_data(id):
    data_val = await db['vm-form'].update_one()

我从调用的函数是:

async def dashboard():
    data_val = session[constants.PROFILE_KEY]
    email = data_val['email']
    user_name = data_val['name']
    response = await retrieve_data(email)
    instance_state = response['instance_state']
    if instance_state == 'running':
        return render_template('dashboard.html',
                               userinfo=session[constants.PROFILE_KEY], vm_data='Hydrogen', vm_type='Windows Instance',
                               userinfo_pretty=json.dumps(session[constants.JWT_PAYLOAD], indent=4))
    else:
        return render_template('dashboard.html',
                               userinfo=session[constants.PROFILE_KEY],
                               userinfo_pretty=json.dumps(session[constants.JWT_PAYLOAD], indent=4))

我不确定这些数据是否足以解决这个错误,但是任何帮助都将不胜感激!

此外,关于如何检查用户是否有权访问实例的任何建议(就像我在仪表板功能中尝试做的那样)都将非常棒!

提前致谢!

共有1个答案

司徒翼
2023-03-14

所以,我让它工作了。当我将requires_auth头放在@app.route()上方时,它工作了(我忘记在问题中添加它了)。

 类似资料: