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:
hello guys
28 Feb 2021, 6:09
Awesome 😎
8 Apr 2021, 16:55
Wowowow
8 Apr 2021, 16:55
Jj
8 Apr 2021, 16:55
Hh
8 Apr 2021, 16:55
H
8 Apr 2021, 16:55
a
26 Apr 2021, 20:33
What the fok are you doing?
4 Jun 2021, 15:24
nice
19 Jun 2021, 13:01
wertyuiop[]
3 Jul 2021, 21:28
yeah
3 Aug 2021, 11:36
wow
6 Aug 2021, 10:42
wertyu
10 Aug 2021, 13:22
alsdfol
30 Aug 2021, 2:17
That isnt me..
15 Sept 2021, 21:43
Very fascinating!
17 Sept 2021, 12:08
e e
27 Sept 2021, 20:04
thats the thing I always wanted dude, you are awesome😎
28 Sept 2021, 4:55
Let's see Paul Allen's Blog
1 Oct 2021, 8:52
f
14 Oct 2021, 17:27
how are you
17 Oct 2021, 10:03
Lol
19 Oct 2021, 18:41
/lol/
19 Oct 2021, 18:41
\lol\
19 Oct 2021, 18:41
|lol|
19 Oct 2021, 18:41
|○|
19 Oct 2021, 18:41
|°l
19 Oct 2021, 18:41
asdfsdf
18 Nov 2021, 16:01
test
18 Nov 2021, 16:01
aaa
6 Dec 2021, 19:42
comment 101
11 Dec 2021, 8:13
HELLO N
17 Dec 2021, 1:58
hola
23 Dec 2021, 19:41
ghjk
7 Jan 2022, 14:55
hjk
7 Jan 2022, 14:55
Nice!
7 Jan 2022, 19:46
I am batman
12 Jan 2022, 10:29
I like it
14 Jan 2022, 14:21
good
22 Jan 2022, 20:33
rtrtrt
31 Jan 2022, 22:50
yuiuop
5 Feb 2022, 16:56
tttttttttttttttt
12 Feb 2022, 17:16
Nice!
8 Mar 2022, 18:41
How would you clean comments if someone wrote something inappropriate?
14 Mar 2022, 2:34
Wagwan
6 Apr 2022, 14:47
I'm gonna make them an offer they can't refuse
6 Apr 2022, 14:47
Nice Work
20 Apr 2022, 19:16
ui
26 Apr 2022, 11:20
i don't give a fucky my name is mariola
29 Apr 2022, 10:04
great
3 May 2022, 13:06
very impressive
3 May 2022, 13:06
dshfksdhfksdf
4 May 2022, 3:33
Let's see Paul Allen's Blogpost
4 May 2022, 8:26
This bussin!
20 May 2022, 15:03
Nice
21 May 2022, 21:59
testtt
26 May 2022, 12:12
Tried
10 Jun 2022, 2:18
Nice. Some formatting would be helpful.
12 Jun 2022, 19:04
testing
16 Jun 2022, 22:14
COol
18 Jun 2022, 4:04
fuck me
21 Jun 2022, 22:02
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
i have no password to create a blog
4 Jul 2022, 3:41
nice
7 Jul 2022, 18:13
This nigga wack
17 Jul 2022, 21:29
fsdfsdfsdf
24 Jul 2022, 16:25
typescript rocks!!
10 Sept 2022, 23:02
hello from teapot
13 Sept 2022, 10:04
yessssssss
14 Sept 2022, 11:15
I have a big big big big dick
14 Sept 2022, 11:15
a
19 Sept 2022, 2:30
ssss
29 Sept 2022, 17:18
asdsa
13 Oct 2022, 2:21
Great blog homie
13 Oct 2022, 17:39
asdf
13 Oct 2022, 17:39
kendall jenner is hot
2 Nov 2022, 19:39
Nice work dude
4 Nov 2022, 17:31
yo
11 Nov 2022, 9:00
Damn, great.
12 Nov 2022, 2:08
Wowowowo, just looking
14 Nov 2022, 2:23
poij
23 Nov 2022, 18:04
spit fire my homie
24 Nov 2022, 1:47
how are you?
30 Nov 2022, 11:40
aa
30 Nov 2022, 11:40
Test Comment @/123-XYZ Test Comment @/123-XYZ Test Comment @/123-XYZ fWS&t1o%MSMdegoB6079
30 Nov 2022, 11:40
good
13 Feb 2021, 19:09