Haider imam
Haider imam

Reputation: 23

sending multiple images from backend(node) to frontend(react)

i have a mern application in which a person can upload his profile pic. Now once the main page of the app loads i want to show all the users along with their profile picture and a little info. For this to happen the server must return multiple images. How do i achieve this in node js. Sending multiple images to the frontend.

Upvotes: 0

Views: 585

Answers (1)

Lukman Isiaka
Lukman Isiaka

Reputation: 71

You can send it to the front-end as a JSON response. Since you didn't provide any code to start with, I assume you are using MongoDB with mongoose.

async function getUsers () { 
   try{
      const data = await User.find()
      const users = data.map((user)=> user)
      res.json({
         users
      })
    }catch(err){
      throw err
   }

The piece of code above returns a JSON response with an array of the user objects if, in your database there an image field, you will get it also in the JSON response while fetching it on the front-end.

Upvotes: 1

Related Questions