0

I get an image by GetObjectCommand in backend (nodejs,express) and then pass to frontend (react) I want to show the image with but data is [object object].

question is how can i convert [object object] to URL for use in img.src?

backend:

const retrieveFile = asyncHandler(async (req, res) => {
  try {
    const param = {
      Bucket: 'my-bucket',
      Key: req.params.id,
    };
    const data = await s3.send(new GetObjectCommand(param));

    res.json(data.Body.toString('utf-8')); //------------------> [object object] sent to frontend
  } catch (err) {
    res.json('Error', err);
  }
});

frontend:

const getImageFromStorage = async () => {
    try {
      const { data } = await axios.get(
        '/api/storage/retrievefile/sadcat.jpg'
      );

      return data; //------------------> [object object] received from backend
    } catch (err) {
      console.error(err);
    }
  };
.
.
.
  <img alt='myimg' src={getImageFromStorage()} /> //------------------> show image
Marzieh Mousavi
  • 413
  • 1
  • 6
  • 17
  • 1
    "how can i convert [object object] to URL" — You can't. It's too late. You've reduced the object to something which doesn't contain the data you desire. Don't convert `data.Body` to that string in the first place. – Quentin Dec 30 '21 at 13:14
  • aww thank you ! – Marzieh Mousavi Dec 30 '21 at 13:47

0 Answers0