0

Hello pls do somebody know how to upload file to MongoDB (mongoose) using nestJS ??

I already have the ability to @Post upload file to my nestJS projet and @Get, but know I wanna post to mongodb using mongoose, pls help

4
  • Do you want to upload a link to the file ? Commented Mar 9, 2021 at 21:57
  • Nop! I want to store images to mongodb but I don’t really know how it works, I have tried many tutorials and it getting one month but I couldn’t figure out! Please help Commented Mar 17, 2021 at 15:24
  • Ok what do you want to know exactly ? How NestJS catches the files or how to upload that file to mongodb? Commented Mar 17, 2021 at 19:05
  • To mongodb only pls!! Commented Mar 18, 2021 at 21:28

3 Answers 3

0

I don't recommend to store images on your database but you can do this:

async function saveFile(file: Express.Multer.File){
//Convert the file to base64 string
const fileB64 = file.buffer.toString('base64')

//userModel is a mongoose model

//Store the string
await this.userModel.create({file: fileB64})
}


async function getFile(userId: string){

//Get user from database
const user = await this.userModel.findOne({_id: userId}).lean()
if(!user) throw new NotFoundException('User not found')

const file = user.file

//Convert the string to buffer
return Buffer.from(file, 'base64')
}

First you have to convert that file to a string with base64 encoding then you can save that string on your database with the create method or updating a document.

If you want to get that file just search that info in your database and then convert the string to buffer and return it.

Like I said before, I don't recommend this it is better if you upload the buffer to s3 and save the link on your database.

Sign up to request clarification or add additional context in comments.

Comments

0

thanks it is worked, but it is only buffer a can't see the image! please is there any others option to get is as image? please here what i'm getting :

{"type":"Buffer","data":[255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,72,0,72,0,0,255,225,0,120,69,120,105,102,0,0,73,73,42,0,8,0,0,0,4,0,18,1,3,0,1,0,0,0,1,0,0,0,49,1,2,0,7,0,0,0,62,0,0,0,18,2,3,0,2,0,0,0,2,0,2,0,105,135,4,0,1,0,0,0,70,0,0,0,0,0,0,0,71,111,111,103,108,101,0,0,3,0,0,144,7,0,4,0,0,0,48,50,50,48,2,160,4,0,1,0,0,0,208,2,0,0,3,160,4,0,1,0,0,0,0,5,0,0,0,0,0,0,255,192,0,17,8,5,0,2,208,3,1,34,0,2,17,1,3,17,1,255,196,0,31,0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,255,196,0,181,16,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,125,1,2,3,0,4,17,5,18,33,49,65,6,19,81,97,7,34,113,20,50,129,145,161,8,35,66,177,193....

4 Comments

Thanks bro, but I couldn’t follow the link explanation , but I also got another option with what you gave to me! Instead of return Buffer.from(file, ‘base64’), i made this : return user. Without convert the buffer string to buffer int then I passed the string to html file in image tag src=‘data:image/jpg;base64, and the string’! And it works!
But the actual problem is when I upload the image from my frontend Angular, I get error code: bad request! As the upload from frontend isn’t based on any model that is why but how to do that?? That what I need now! I really appreciated your help it was very very helpful! Thanks bro thanks bro thanks a lot !!! It’s take a month to get the solution work! Thanks!
Can you add more info about how are you sending the request ? And your nestjs controller. Attach this info on the question and let me know.
0

Angular service file

postFile(fileToUpload: File): Observable<any> {

const formaData: FormData = new FormData();
formaData.append('fileKey', fileToUpload, fileToUpload.name);
return this.http.post(`${this.backEndURL}/api/prods/upload/two/tree/`, JSON.stringify(formaData));}

but my backend Nestjs trow error:

[ExceptionsHandler] Cannot read property 'buffer' of undefined +80859ms

TypeError: Cannot read property 'buffer' of undefined

1 Comment

Lastly, I changed the file to base64 at the front end side and then send it directly as string to my backend nestJS and get it saved to mongodb but I understood why you said not recommending saving it to data, because I have only saved 3 file as buffer.toString, but my frontend take too long to view the page content! So need that s3 option! and I seen many questions related but not way to save the link to database, so please if you have any idea! Please help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.