Python/Bottle/MongoDB:不支持的响应类型:
@route('/locations', method='GET')
def get_location():
entity = db['locations'].find({'coordinate2d': {'$near': [37.871593, -122.272747]}}).limit(3)
if not entity:
abort(404, 'No nearby locations')
return entity
上述代码部分的响应是:
Error 500: Internal Server Error
Sorry, the requested URL 'http://localhost:8080/locations' caused an error:
Unsupported response type: <type 'dict'>
如何从 mongo 获取信息作为 Bottle 可以返回 JSON 的类型?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
完整的解决方案是将数据库游标转换为列表、手动设置响应类型+自定义编码返回值的组合
在我的例子中,产生以下结果:
The complete solution was a combination of transforming the db cursor to a list, manually setting the response type + custom encoding the return value
In my case, produces this:
当我尝试返回 python 列表时出现此错误。我以为它会转换为 JSON,但事实并非如此。它到达了 Bottle.py 中处理可迭代对象的行,并找到了列表中的第一个字典并抛出了上面的错误。
为了解决这个问题,我只是将列表嵌入到字典中。
I got this error when I was trying to return a python list. I assumed it would translate into JSON, but it didn't. It made it to the line in bottle.py where it would handle iterables and found the first dict in the list and threw the error above.
To get around this, I simply embedded my list inside a dict.
根据瓶子上的文档提到 http://bottlepy.org/docs/dev/ 你有从 @route 装饰器返回字符串。您必须返回带有数据或字符串的模板。
如果您想生成 json,则必须更改
Content-Type
。http://bottlepy.org/docs/dev/tutorial。 html?highlight=json#生成内容
As per the doc mention on bottle http://bottlepy.org/docs/dev/ you have to return the string from the
@route
decorator. You have to return the template with data or string.If you want to generate the json then you have to change the
Content-Type
.http://bottlepy.org/docs/dev/tutorial.html?highlight=json#generating-content