Showing the Avatar, Connecting the Dot

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 354:- Showing the Avatar, Connecting the Dot

I assume that "Connecting the Dot" here means to connect the uploaded avatar image to the user's account/profile. To do this, we need to update the user's document in the database with the file path of the uploaded avatar image.

Here are the general steps to connect the uploaded avatar image to the user's account/profile:

  1. Update the user's schema to include a new field for storing the avatar image file path. For example:

arduinoCopy code

const userSchema = new mongoose.Schema({  // existing fields  avatar: {    type: String  } });

  1. When a user uploads an avatar image, save the file to a directory on the server using Multer.
  2. After the file has been saved, get its file path and update the user's document in the database with the file path. You can do this by finding the user by their ID and then using the updateOne method to set the avatar field to the file path. For example:

csharpCopy code

router.post('/upload-avatar', upload.single('avatar'), async (req, res) => {  // Get the file path of the uploaded avatar image  const avatarPath = req.file.path;  // Update the user's document with the file path  const userId = req.user._id;  await User.updateOne({ _id: userId }, { avatar: avatarPath });  // Redirect to the user's profile page  res.redirect('/profile'); });

  1. In the user's profile page, display the avatar image using the file path stored in the avatar field of the user's document. For example:

phpCopy code

<img src="{{ user.avatar }}" alt="Avatar">

Note that you need to pass the user object (which includes the avatar field) to the view when rendering the user's profile page.

41. File Upload

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

I am not able to access videos from second class and further. I have already completed first class

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?