Answer the question
In order to leave comments, you need to log in
How to add an avatar for a user?
I’m working on a project for myself and at the same time I’m studying express / nodejs, I made registration simple, name, email, password, now I’ve thought about everything and I want to add the ability to add an avatar for users, but I still don’t quite understand how to implement it, I can make a post request for a separate route and to resave the user already with a new key? how then not to affect the old name, email, password, and update only the avatar, I use mongodb and mongoose for the node
Answer the question
In order to leave comments, you need to log in
Break down the task into several steps:
1) Accept the uploaded file from the user.
2) Check that this file is an image and its size is not higher than allowed (if it is higher, then either reject the download and inform the user about an error, or reduce the size and save the modified file).
3) If point 2 is passed successfully - save this downloaded file to disk.
4) If point 3 is passed successfully - set for the user who uploaded this file, an additional field (named userpic, for example), indicating the name of the file that was saved in the previous paragraph. Remember the previous value of this field for the next step.
5) If step 4 is successful, delete the file with the previous user's avatar from the disk. If with an error - then delete the new downloaded file from the disk, leave the old one.
6) Unsubscribe to the user that the avatar has been changed, or that it has not been changed, depending on the result.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question