【问题标题】:how to resolve a TypeError: Object of type ObjectId is not JSON serializable using flask-jwt如何解决 TypeError:ObjectId 类型的对象不能使用 flask-jwt 进行 JSON 序列化
【发布时间】:2021-11-07 11:49:53
【问题描述】:

我有一个无法解决的错误。这是我在使用邮递员进行身份验证时遇到的错误:TypeError: ObjectId type of object is not JSON serializable // Werkzeug Debugger 文件“C:\Users\Amoungui\AppData\Local\Programs\Python\Python39\Lib\json\encoder.py”,第 179 行,默认 raise TypeError(f'Object of type {o.class.name} ' TypeError:ObjectId 类型的对象不是 JSON 可序列化的

这是我的代码,我遵循了官方文档,但在这不起作用我不明白。这是文档的链接:https://pythonhosted.org/Flask-JWT/

customer.py

from flask import jsonify, make_response
from config.mongoose import db
import bson 

class Customer(db.Document):
    _id = db.ObjectIdField(default=bson.ObjectId, primary_key=True) #bson.ObjectId
    tel = db.StringField()
    password = db.StringField()
    
    def to_json(self):
        return {
            "_id": self._id,
            "tel": self.tel,
            "password": self.password,
        }
     
    def findAll(self):
        users = []
        for user in self.objects:
            users.append(user)
            
        return users

service.py

from Models.Customer import Customer
from werkzeug.security import safe_str_cmp

find_by_username = {u.tel:u for u in Customer.objects}

find_by_id = {u._id: u for u in Customer.objects}

def auth(username, password):
    user = find_by_username.get(username, None)
    if user and safe_str_cmp(user.password.encode('utf-8'), password.encode('utf-8')):
        return user
    
def identity(payload):
    _id = payload['identity']
    return find_by_id.get(_id)  

感谢您的帮助

【问题讨论】:

    标签: python json flask-restful werkzeug flask-jwt


    【解决方案1】:

    我这样更改客户类

    from flask import jsonify, make_response
    from config.mongoose import db
    import bson 
    
    class Customer(db.Document):
        _id = db.ObjectIdField() #bson.ObjectId
        tel = db.StringField()
        password = db.StringField()
        
        def to_json(self):
            return {
                "_id": self._id,
                "tel": self.tel,
                "password": self.password,
            }
         
        def findAll(self):
            users = []
            for user in self.objects:
                users.append(user)
                
            return users
    

    我有新的错误: identity = getattr(identity, 'id') 或 identity['id'] getitem
    中的文件“C:\Users\Amoungui\AppData\Local\Programs\Python\Python39\Lib\site-packages\mongoengine\base\document.py”,第 250 行 引发 KeyError(名称) KeyError: 'id'

    【讨论】:

      【解决方案2】:

      错误消息表明序列化bson.ObjectId 存在问题。您是否尝试过以下建议的解决方案:type error objectid

      【讨论】:

      • 我像这样更改客户类``` id = db.ObjectIdField() #bson.ObjectId``` 我有新错误:identity = getattr(identity, 'id') 或身份['id'] 文件“C:\Users\Amoungui\AppData\Local\Programs\Python\Python39\Lib\site-packages\mongoengine\base\document.py”,第 250 行,在 __getitem_ raise KeyError(name) KeyError: 'id'
      猜你喜欢
      • 2021-04-08
      • 1970-01-01
      • 2019-10-27
      • 2021-03-23
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2021-09-24
      • 2020-09-05
      相关资源
      最近更新 更多