Ji Yaodian

TypeError("'ObjectId' object is not iterable")

‘ObjectId’ object is not iterable If you use fastapi, when the return result contains the id of mongodb, which is the ObjectId type, the error message will be reported: TypeError("‘ObjectId’ object is not iterable"). A direct Google search can be used to get several methods: Use the str() method to convert the ObjectId to a string Use the built-in json_util.dumps() method of bson to convert the ObjectId to a string Delete the ObjectId field Define a JSONEncoder class to convert the ObjectId to a string 1 2 3 4 5 6 7 8 9 10 import json from bson import ObjectId class JSONEncoder(json....

June 5, 2024 · 3 min · jyd