简体   繁体   English

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

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

I am new to python, I am trying to add data into a collection:我是 python 新手,我正在尝试将数据添加到集合中:

import pymongo 
from bson.objectid import ObjectId

client = pymongo.MongoClient("mongodb://localhost:27017/")
db = client['inventory']
collection = db['items']

class Inventory():

     def insert(self, data):
        response = collection.insert_one(data)
        return response.inserted_id

However I keep getting the same error:但是我不断收到同样的错误:


Traceback (most recent call last):
  File "/home/vitor/.local/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/vitor/.local/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
    return self.finalize_request(rv)
  File "/home/vitor/.local/lib/python3.7/site-packages/flask/app.py", line 1967, in finalize_request
    response = self.make_response(rv)
  File "/home/vitor/.local/lib/python3.7/site-packages/flask/app.py", line 2130, in make_response
    " {rv.__class__.__name__}.".format(rv=rv)
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 TypeError.

I believe the solution is simple, but I can't seem to find it.我相信解决方案很简单,但我似乎找不到它。 I wold appreciate any help.我会很感激任何帮助。

EDIT:编辑:

On request of amanb , here are my routes and index.根据amanb要求,这是我的路线和索引。

index:指数:

from flask import Flask, request
from src.routes.inventory import inventory_route

app.register_blueprint(inventory_route, url_prefix='/inventory')

if __name__ == '__main__':
    app.run(debug=True)

routes:路线:


from flask import Flask, Blueprint
from src.controllers.inventory import inventoryController

controller = inventoryController()

inventory_route = Blueprint('inventory_route', __name__)

@inventory_route.route("/addItem", methods=['POST'])
def addItem():
    response = controller.addItem()
    return response

After a couple of more tries, I've discovered that the data being passed was wrong for some reason.经过几次尝试,我发现由于某种原因传递的数据是错误的。 I was using Insomnia to pass data but for some mysterious reason the data inside the request body was giving this error.我正在使用Insomnia传递数据,但出于某种神秘的原因,请求正文中的数据出现了此错误。 I just hard codded the data into the insert_one() method and it worked.我只是将数据硬编码到insert_one()方法中并且它起作用了。

response = collection.insert_one({ "name": "new Item" })

暂无
暂无

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

相关问题 TypeError:返回类型必须是字符串、字典、元组、响应实例或 WSGI 可调用的,但它是协程 - TypeError: The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a coroutine 类型错误:返回类型必须是字符串、字典、元组、响应实例或可调用的 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 类型错误:视图 function 未返回有效响应。 返回类型必须是字符串、字典、元组、Response 实例,但它是一个 int - TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, but it was a int 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 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