Skip to content

Conversation

@janetat
Copy link
Contributor

@janetat janetat commented Jun 22, 2020

现象

当MongoDB没有数据时,第一次“添加人员”会报错。

IndexError: no such item for Cursor instance

原因

定位在anwer/DataBaseManager.DataBaseManager中的_query_last_id。

def _query_last_id(self):
    last_info = self.handler.find({}, {'_id': 0, 'id': 1}).sort('id', -1).limit(1)
    return last_info[0]['id'] if last_info else 0   # last_info是一个cursor,不管有没有数据,if last_info都为True。

解决办法

return last_info[0]['id'] if last_info.count() else 0

Copy link
Owner

@kingname kingname left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方可以改成
last_info = self.handler.find_one({}, {'_id': 0, 'id': 1}, sort=[('_id', -1)])
return last_info['id'] if last_info else 0

@kingname kingname merged commit 99d0c2e into kingname:master Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants