Scene description
After the MongoDB database is used to insert data, an error is reported when the flash restful interface is used to return data:
TypeError: Object of type ObjectId is not JSON serializable
Cause analysis
The reason for this problem is that when data is written to MongoDB, even if you do not specify a _id
field, a field will be automatically generated for each piece of data _id
, for example:
{
"_id" : ObjectId("6180af3ef261f0827ea248d6"),
"省份名称" : "澳门",
"省份链接" : "https://www.gongkaoleida.com/area/3509",
"省份代号" : "3509"
}
This field is of ObjectId
type and cannot be returned using JSON serialization, so an error will be reported
Solution:
Since _id
field cannot be converted and has no effect on our actual data. Then pop it up after inserting the data. An example is as follows:
self.db.filter_result.insert_one(item)
item.pop('_id')
Read More:
- TypeError: ‘WebElement’ object is not iterable error
- Json: struct field readyReplicas has json tag but is not exported [How to Solve]
- Taro Use React Hooks Error: TypeError: Object(…) is not a function
- Webpack packaging error TypeError: this.getOptions is not a function at Object.lessLoader
- [Solved] SpringBoot Error: HttpMediaTypeNotSupportedException: Content type ‘application/json‘ not supported
- JSON parse error: Can not deserialize instance of java.lang.String out of START_OBJECT token
- Libtorch Error: Expected object of type Variable but found type CUDALongType for argument #2 ‘index’
- [Solved] MongoDB Error: FaileError: dToParse: Password must be URL Encoded for mongodb:// URL:
- TypeError: db.collection is not a function
- Abnormal [System.InvalidOperationException: custom type mapping for ‘xxx’ is not specified or is not a solution
- [Solved] TypeError: super(type, obj): obj must be an instance or subtype of type
- TypeError: res.render is not a function
- [Solved] MindSpore Error: Data type conversion of ‘Parameter’ is not supporte
- [Solved] MindSpore Error: TypeError: For ‘TopK’, the type of ‘x’ should be…
- maven Error: Element ‘dependency’ cannot have character [children], because the type’s content type is
- Syntax Error: TypeError: this.getOptions is not a function
- std::dynamic_pointer_cast Error: source type is not polymorphic
- Django Error: ‘set‘ object is not reversible [How to Solve]