Alven Walker

This post will teach you how to store images into a MongoDB database using the Mongoose ODM as well as retrieve and display those images. To begin, we will create our server file. First we must install and require several npm packages. var express = require(‘express’); var fs = require(‘fs’); var mongoose = require(‘mongoose’); var Schema = mongoose.Schema; var multer = require('multer'); Next, we will connect to your MongoDB using the URL that your database is found at: mongoose.connect(‘url_here’); Once we have established a connection to our database and required all the necessary packages, we can now begin defining our server-side logic. We must first define our database schema var Item = new ItemSchema( { img: { data: Buffer, contentType: String } } ); var Item = mongoose.model('Clothes',ItemSchema); The important takeaway here is that our data type is a Buffer, which allows us to store our image as data in the form of arrays. Next we must define the path of the image we are uploading. Here, we are using the middleware Multer to upload the photo on the server side. app.use(multer({ dest: ‘./uploads/’, rename: function (fieldname, filename) { return filename; }, })); Here, we use Multer to take a photo and put it in a folder called ‘uploads’ so we can easily access it later. Finally, we must handle our post request to our database. app.post(‘/api/photo’,function(req,res){ var newItem = new Item(); newItem.img.data = fs.readFileSync(req.files.userPhoto.path) newItem.img.contentType = ‘image/png’; newItem.save(); }); Here, we create an instance of our Item model, and save the attributes of img.data and img.contentType. The img.data key will have a value of the image data, which will be an array of numbers representing our actual image. The img.contentType key specifies that we specifically have a png file. Finally, calling the .save() method will save this instance of our Item to our database. If you check your database find img.data as <Binary Data> that means you’re all set! You’ve successfully saved an image to a database.

Comments:

me

good

13 Feb 2021, 19:09

Kanye West

hello guys

28 Feb 2021, 6:09

Wow

Awesome 😎

8 Apr 2021, 16:55

W

Wowowow

8 Apr 2021, 16:55

O

Jj

8 Apr 2021, 16:55

U

Hh

8 Apr 2021, 16:55

U

H

8 Apr 2021, 16:55

lo&#x27;l

a

26 Apr 2021, 20:33

Smokes

What the fok are you doing?

4 Jun 2021, 15:24

Yodo

nice

19 Jun 2021, 13:01

oiuytr

wertyuiop[]

3 Jul 2021, 21:28

biden tastes like banana sauce

yeah

3 Aug 2021, 11:36

yeahh

wow

6 Aug 2021, 10:42

aswertyh

wertyu

10 Aug 2021, 13:22

but how to store images tho

alsdfol

30 Aug 2021, 2:17

Kanye ewst

That isnt me..

15 Sept 2021, 21:43

Doops

Very fascinating!

17 Sept 2021, 12:08

eee

e e

27 Sept 2021, 20:04

Thanks

thats the thing I always wanted dude, you are awesome😎

28 Sept 2021, 4:55

Pyscho

Let&#x27;s see Paul Allen&#x27;s Blog

1 Oct 2021, 8:52

f

f

14 Oct 2021, 17:27

HELLO

how are you

17 Oct 2021, 10:03

Lol

Lol

19 Oct 2021, 18:41

Lol

&#x2F;lol&#x2F;

19 Oct 2021, 18:41

Lol

&#x5C;lol&#x5C;

19 Oct 2021, 18:41

Lol

|lol|

19 Oct 2021, 18:41

Lol

|○|

19 Oct 2021, 18:41

Lol

|°l

19 Oct 2021, 18:41

tasfsdf

asdfsdf

18 Nov 2021, 16:01

test

test

18 Nov 2021, 16:01

ttt

aaa

6 Dec 2021, 19:42

test101

comment 101

11 Dec 2021, 8:13

Genésio

HELLO N

17 Dec 2021, 1:58

concha de tu madre

hola

23 Dec 2021, 19:41

fghj

ghjk

7 Jan 2022, 14:55

nj

hjk

7 Jan 2022, 14:55

Cool?

Nice!

7 Jan 2022, 19:46

yo

I am batman

12 Jan 2022, 10:29

Nice

I like it

14 Jan 2022, 14:21

good

good

22 Jan 2022, 20:33

uuyuy

rtrtrt

31 Jan 2022, 22:50

rtttt

yuiuop

5 Feb 2022, 16:56

ttttttttttttt

tttttttttttttttt

12 Feb 2022, 17:16

Odin User

Nice!

8 Mar 2022, 18:41

Someone

How would you clean comments if someone wrote something inappropriate?

14 Mar 2022, 2:34

Bjdljkhfojh

Wagwan

6 Apr 2022, 14:47

Vito Corleone

I&#x27;m gonna make them an offer they can&#x27;t refuse

6 Apr 2022, 14:47

Me

Nice Work

20 Apr 2022, 19:16

iuo

ui

26 Apr 2022, 11:20

ola

i don&#x27;t give a fucky my name is mariola

29 Apr 2022, 10:04

xuanthang

great

3 May 2022, 13:06

someone else

very impressive

3 May 2022, 13:06

kdsfhk

dshfksdhfksdf

4 May 2022, 3:33

Very nice

Let&#x27;s see Paul Allen&#x27;s Blogpost

4 May 2022, 8:26

Parkr

This bussin!

20 May 2022, 15:03

Nice

Nice

21 May 2022, 21:59

test

testtt

26 May 2022, 12:12

Try

Tried

10 Jun 2022, 2:18

B.Dd

Nice. Some formatting would be helpful.

12 Jun 2022, 19:04

test

testing

16 Jun 2022, 22:14

Bro

COol

18 Jun 2022, 4:04

broski

fuck me

21 Jun 2022, 22:02

FUCK ME HARDER DADDY

NO AUTHORITIES HERE, YOU DIDNT VALIDATE INPUT SO NOW IM GOING TO CRASH THIS ENTIRE MOTHA FUCKING THING DOWN!!!!!!!!!!!!!!!! MUWAHAHSHAHAHAHAHAHAHA

21 Jun 2022, 22:02

mohamed

i have no password to create a blog

4 Jul 2022, 3:41

franco

nice

7 Jul 2022, 18:13

Yoyos

This nigga wack

17 Jul 2022, 21:29

yrdy

fsdfsdfsdf

24 Jul 2022, 16:25

lets see if it works

typescript rocks!!

10 Sept 2022, 23:02

teapot

hello from teapot

13 Sept 2022, 10:04

bighead

yessssssss

14 Sept 2022, 11:15

bigdick

I have a big big big big dick

14 Sept 2022, 11:15

a

a

19 Sept 2022, 2:30

a

ssss

29 Sept 2022, 17:18

lol

asdsa

13 Oct 2022, 2:21

hubs

Great blog homie

13 Oct 2022, 17:39

asdf

asdf

13 Oct 2022, 17:39

interesting

kendall jenner is hot

2 Nov 2022, 19:39

alex

Nice work dude

4 Nov 2022, 17:31

yo

yo

11 Nov 2022, 9:00

mart1d4

Damn, great.

12 Nov 2022, 2:08

Olkyy

Wowowowo, just looking

14 Nov 2022, 2:23

j

poij

23 Nov 2022, 18:04

Big Dave

spit fire my homie

24 Nov 2022, 1:47

BarSnop

how are you?

30 Nov 2022, 11:40

BarSnop

aa

30 Nov 2022, 11:40

Test Comment @&#x2F;123-XYZ

Test Comment @&#x2F;123-XYZ Test Comment @&#x2F;123-XYZ Test Comment @&#x2F;123-XYZ fWS&amp;t1o%MSMdegoB6079

30 Nov 2022, 11:40