[英]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.