0

I'm getting a string from the database that has base64 encoded Image like this res.send(user.img). here is what I'm getting:

JSON from the server

All I want is to get this string:

The string that I'm trying to get

But here is what happens when I try to specify the value that I need like this res.send(user.img.data):

data transforms into crazy gibbersih

Crazy, right? I just want to get that string and for it to be normal text and not gibberish. Please, help.

Here is how the object looks in the DB (notice how it looks properly here):

Object

Here is the fetch:

app.post("/getImage", (req, res)=>{
   User.findOne({ login: "name" })
     .then((user) => res.send(user.img.data))
     .catch((error) => {
       console.log(error);
       res.send(500)
     });
 })

Here is what i'm doing in Postman:

Postman

  • Is this what are you looking for? https://stackoverflow.com/questions/2820249/base64-encoding-and-decoding-in-client-side-javascript – Eloi Sep 07 '21 at 08:40
  • I'm not trying to decode/encode anything. All I'm trying to do is to get that string that I already have and for it to not be symbols. – King Of Chads Sep 07 '21 at 08:43
  • 1
    Please show your JS code, the code that fetches, logs and accesses the server's reply. –  Sep 07 '21 at 08:45
  • Here is the fetch: ``app.post("/getImage", (req, res)=>{ User .findOne({ login: "name" }) .then((user) => res.send(user.img.data)) .catch((error) => { console.log(error); res.send(500) }); })`` – King Of Chads Sep 07 '21 at 08:47
  • Added the DB object in the original post. – King Of Chads Sep 07 '21 at 08:50
  • 1
    We need to see your *client-side* code. You posted a node/express route handler, i.e. server-side code. –  Sep 07 '21 at 08:51
  • I understand but there is obviously no client side code because I'm using postman for testing. Added in the OP – King Of Chads Sep 07 '21 at 08:52
  • Ok. It says right in your image that it's binary data. You're seeing a base64 representation because otherwise you'd get exactly the "gibberish" you see in postman. That gibberish is binary code represented using ASCII characters. In the end this isn't really about base64; you can send binary data to the client and display it as image. –  Sep 07 '21 at 08:54
  • That's very cool, man, but what am I supposed to do? I just want to get that string, nothing crazy about that. – King Of Chads Sep 07 '21 at 08:55
  • No, but if you want to send a base64 string, you have to create it first. If this is an [xy problem](https://xyproblem.info/) though and what you ultimately want to do is display the image in your web app, you don't need to do that. You just send the file using the proper mime-type: https://stackoverflow.com/questions/30212813/express-return-binary-data-from-webservice. Don't get condescending because you don't understand what's happening. –  Sep 07 '21 at 08:57
  • You can either A) [turn that data into a base64 string](https://nodejs.org/api/buffer.html#buffer_buf_tostring_encoding_start_end) and simply send it back using `res.send(base64String)` or B) send the binary data as-is using proper headers and use the URL directly in a `` –  Sep 07 '21 at 09:02
  • Sorry, man, I don't think your solution is correct. Tried: ``res.send(Buffer.from(user.img.data)`` and it's just the same gibberish as before. SAD. I can't use the second solution because I literally have no idea what MIME type is. – King Of Chads Sep 07 '21 at 09:13
  • Afaik it should be `res.send(Buffer.from(user.img.data).toString("base64"))`. Mimetype means adding a header like `Content-Type: image/jpg`. But again, what is the ultimate goal here? Display an image from the db in a web app? Turning it into base64 first might not be necessary at all. Here's how to serve a binary download in express: https://stackoverflow.com/a/30625085/5734311 –  Sep 07 '21 at 09:21
  • I did a quick test and provided there's a proper content-type header, postman will display the image instead. –  Sep 07 '21 at 09:26

0 Answers0