我有一个正在处理的小型 API,一切正常,我的所有请求都按预期执行,但是当我尝试通过 URL 查询过滤结果时,由于某种原因它适用于 id 但不适用于设备字段。
def on_get(self, req, resp):
"""Handles GET requests"""
if req.get_param("id"):
result = {'location': r.db(PROJECT_DB).table(PROJECT_TABLE).get(req.get_param("id")).run(db_connection)}
elif req.get_param("device"):
result = {'location': r.db(PROJECT_DB).table(PROJECT_TABLE).get(req.get_param("device")).run(db_connection)}
else:
location = r.db(PROJECT_DB).table(PROJECT_TABLE).run(db_connection)
result = {'locations': [i for i in location]}
resp.body = json.dumps(result)
示例http://localhost:8000/location?id=(some random id)
这将起作用
但如果我这样做http://localhost:8000/location?device=(some device)
将不起作用,返回 null
那么谁能告诉我我做错了什么?或者更好的是,如果有人知道使用 URL 进行过滤的更好方法?
注意:我正在使用 rethinkdb
编辑:
这是我通常拥有的:
{
"locations": [
{
"id": "4bf4b94f-747a-42db-9d54-a8399d995025",
"location": "gps coords",
"device": "Device 2"
},
{
"id": "b5cce561-37d2-42e7-86e4-a31c008b0af2",
"location": "gps coords",
"device": "Device 1"
},
{
"id": "bebba7cf-710c-4ee8-ad69-2d58174d4e02",
"location": "gps coords",
"device": "Device 1"
},
{
"id": "e928f84b-60ff-40f3-b839-920bc99e5480",
"location": "gps coords",
"device": "Device1"
}
]
}
按 id 过滤可以正常工作,但不能按设备过滤,这很奇怪