简体   繁体   English

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

[英]TypeError: The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a coroutine

I am working on flask and MongoDB to create and start AWS EC2 instances and log that for different users.我正在研究 flask 和 MongoDB 以创建和启动 AWS EC2 实例并为不同的用户记录这些实例。 But when I try to call a function that I wrote to retrieve a document, this error is returned:但是当我尝试调用我为检索文档而编写的 function 时,会返回此错误:

TypeError: The view function did not return a valid response. TypeError:视图 function 未返回有效响应。 The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a coroutine.返回类型必须是字符串、字典、元组、响应实例或 WSGI 可调用的,但它是协程。

Traceback:追溯:

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 -

My DB code is:我的数据库代码是:

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()

And function from where I am calling is:我打电话的地方是 function:

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))

I am not sure if this is enough data to solve the error but any help would be greatly appreciated!我不确定这是否有足够的数据来解决错误,但任何帮助将不胜感激!

Also any suggestions on how to go about checking if the user has access to an instance(like I am trying to do in the dashboard function) would be awesome!此外,任何关于如何检查用户是否有权访问实例的 go 的建议(就像我在仪表板功能中尝试做的那样)都很棒!

Thanks in advance!提前致谢!

So, I've got it to work.所以,我已经开始工作了。 It worked when I put the requires_auth header above the @app.route() (which I forgot to add in the question).当我将 requires_auth header 放在 @app.route() (我忘了在问题中添加)上方时,它起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回类型必须是字符串、字典、元组、响应实例或可调用的 WSGI,但它是一个 TypeError - The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a TypeError 类型错误:返回类型必须是字符串、字典、元组、响应实例或可调用的 WSGI,但它是一个列表 - Type Error: The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a list 查看 function 未返回有效响应。 返回类型必须是字符串、字典、元组、响应实例或 WSGI 可调用,但它是一个列表 - 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 list python中的oracle查询-返回类型必须是字符串、字典、元组 - oracle query in python - The return type must be a string, dict, tuple Python中的dict()问题,TypeError:'tuple'对象不可调用 - Issue with dict() in Python, TypeError:'tuple' object is not callable 类型错误:元组索引必须是整数或切片,而不是 dict - TypeError: tuple indices must be integers or slices, not dict TypeError:“协程”对象不可调用 - TypeError: 'coroutine' object is not callable PyMongo:TypeError:spec必须是dict的一个实例 - PyMongo: TypeError: spec must be an instance of dict 应用调用。 类型错误:在 include() 的情况下,视图必须是可调用的或列表/元组 - application call . TypeError: view must be a callable or a list/tuple in the case of include() Django URLs TypeError: view must be a callable or a list/tuple in the case of include() - Django URLs TypeError: view must be a callable or a list/tuple in the case of include()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM