from sanic_motor import BaseModel class UserModel(FBaseModel): __coll__ = 'user' cursor = await UserModel.find({}, {"person_info": 0}, sort=[{'score', -1}], as_raw=True, page=1, per_page=10) # 传参后,sort变成[{ -1,'score'}]
以上代码段在执行时经常报以下错误:
File "D:\developer\pingche\pinche_server\venv\lib\site-packages\pymongo\helpers.py", line 99, in _index_document
raise TypeError("first item in each key pair must be a string")
TypeError: first item in each key pair must be a string
长期都无法发现错误所在,后经调试单步测试,结合AI分析,原来是这里写错了
sort=[{'score', -1}]
应该改为以下
sort=[('score', -1)]
由于在传参时,sort=[{'score', -1}] 会一定几率变成sort=[{ -1,'score'}]
from sanic_motor import BaseModel class UserModel(FBaseModel): __coll__ = 'user' cursor = await UserModel.find({}, {"person_info": 0}, sort=[('score', -1)], as_raw=True, page=1, per_page=10) # 传参后,sort变成[{ -1,'score'}]
写错了还可以正常运行,随机性出错,导致这种错误最难发现,谨此记录。
评论