gothinkster / flask-realworld-example-app

Exemplary real world JSON API built with Flask (Python)
https://realworld.io/
MIT License
897 stars 308 forks source link

KeyError: 'profile' and KeyError: 'user' #20

Open leros opened 5 years ago

leros commented 5 years ago

I am running the app in dev model. I tried to add a new article for a new user and got KeyError: 'profile'which was due to a problem of this code:

  .../flask-realworld-example-app/conduit/profile/serializers.py", line 18, in make_user
    return data['profile']`

Also, I got another key error when trying to update user profile:

  File "/Users/demo/learn-fullstack/flask-realworld-example-app/conduit/user/serializers.py", line 21, in make_user
    data = data['user']
KeyError: 'user'
przor3n commented 4 years ago

so, if you are still looking for solution, then you have to comment out @pre_load decorator in profile's serializer.py. this will give you and error, that is solved by adding author=None as an argument to article view function make_article. this makes adding articles possible, user is set.

you can remove the @pre_load decorator from user/serializer.py, this will show you an error 422. i'm still struggling with it.

przor3n commented 4 years ago

serializers for user and profile assume that every time frontend sends {'user': #payload } or {'profile': #payload }, but update and create article send only dict with payload keyvals. so solution is to check if the user and profile key is in data, in functions decorated with pre_load (do something like this: return data.get('profile', data))

still, add that author=None to make_article.

for updating, you also need to allow for password to be empty in UserSchema. ( password = fields.Str(load_only=True, allow_none=True) ) I think it need new schema for updating user profile.