0

I'm working on mern project and need to find a way to store images in the project directory. The images are originally stored in the database. Once I obtain the images I need to save them in project folder like assets. Is there a way to do this with just JavaScript?

abelito
  • 1,094
  • 1
  • 7
  • 18
Zephyr
  • 1,612
  • 2
  • 13
  • 37

1 Answers1

0

If you are writing server-side javascript code that queries a database, then yes. React code is typically for creating client side/front end code that runs within a browser however and won't be able to interact with a server's database except through APIs.

From your edit saying you're using Express and MongoDB, it sounds like you should be able to use the following links to guide you in accomplishing what you're trying to do:

Store images in Mongodb serve them with Nodejs

abelito
  • 1,094
  • 1
  • 7
  • 18
  • I guess my question isn't very clear. My images are originally stored in mongodb. I have an API that will fetch the images when I need them. But once I have them I need to store it in my project folder. I have some images stored in my project directory in assets folder. I want to store the images I get from mongodb in that assets folder. – Zephyr May 27 '19 at 11:54
  • So it sounds like you need to pull the binary data from MongoDB and use the basic file system IO calls to write the file to the assets folder https://stackoverflow.com/questions/43487543/writing-binary-data-using-node-js-fs-writefile-to-create-an-image-file That same link above should help guide you in the initial pull of the binary data from Mongo – abelito May 27 '19 at 12:10