0

I have an error that I cannot resolve. here is the error I get when I authenticate with postman: TypeError: Object of type ObjectId is not JSON serializable // Werkzeug Debugger File "C:\Users\Amoungui\AppData\Local\Programs\Python\Python39\Lib\json\encoder.py", line 179, in default raise TypeError(f'Object of type {o.class.name} ' TypeError: Object of type ObjectId is not JSON serializable

Here is my code, I followed the official documentation, but at this does not work I do not understand. here is the link of the documentation: 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)  

thank's for your help

2 Answers2

0

The error message suggests its an issue with serializing bson.ObjectId. Have you tried solutions suggested in: type error objectid

marcel h
  • 742
  • 1
  • 7
  • 20
  • I change the customer class like this ``` _id = db.ObjectIdField() #bson.ObjectId``` I have the new error: **identity = getattr(identity, 'id') or identity['id'] File "C:\Users\Amoungui\AppData\Local\Programs\Python\Python39\Lib\site-packages\mongoengine\base\document.py", line 250, in __getitem__ raise KeyError(name) KeyError: 'id'** – Amoungui Serge Sep 12 '21 at 12:04
0

I change the customer class like this

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

I have the new error: identity = getattr(identity, 'id') or identity['id'] File "C:\Users\Amoungui\AppData\Local\Programs\Python\Python39\Lib\site-packages\mongoengine\base\document.py", line 250, in getitem
raise KeyError(name) KeyError: 'id'