#Nestjs-form-data not serializing.

25 messages · Page 1 of 1 (latest)

crimson violet
#

Hello,
I've implemented nestjs-form-data for managing files and validations but it actually doesn't work .

brazen comet
#

try with raw, json

crimson violet
#

I will upload a file sometimes

wary flame
#

multer doesn't automatically parse the data the same way json will. You'll need some custom @Transform()s for that

crimson violet
#

yeah but nestjs-form-data package provides that feature, does not?

wary flame
#

Not sure honestly. Looks like it uses append-field, so it might depend on how that librarty works. Never worked with nestjs-form-data to be honest

toxic gate
#

i've been using nestjs- form-data in my project as well and what i've figured is that when doing the request as multipart/form-data all values will be received as strings and you have to use @Transform() to handle them in your DTO.

i'm not sure if thats an issue with the library or if its just how multipart/form-data works tbh

crimson violet
#

Thanks, I'm trying this, but how to transform an array?

wary flame
#

How do you send it, how does it come in?

crimson violet
wary flame
#

Remove the validation momentarily to see what the data looks like after busboy parses it

crimson violet
wary flame
#

I believe a JSON.parse() will do for that

crimson violet
#

It's done, thanks for help.

#

But how it's usually manages file uploads with other types of data. In a single request or multiple (json)?

wary flame
#

Most people I know try to avoid multi file uploads for the complexity of it, but it is a valid use case, so it all just depends

crimson violet
#

is there any option to upload files?

wary flame
#

What do you mean?

crimson violet
#

that it is easier for validations

wary flame
#

Your issue is with how multipart/form-data is parsed. These parsers really exist to handle the binary data of forms and treat absolutely everything as strings. I've not looked into if there is a parser that treats binary as binary and everything else parsed into a JS type like you're wanting

#

But good news, this workaround exists, and it's honestly not too complex

crimson violet
#

ah okay I understand, thanks for help.

toxic gate
wary flame
#

It depends on how you want to do the files. Does each file have it's own name and description? Are you just adding a name and description for the album instead? Uploading multiple files at once isn't impossible, nor is it bad, the "difficult" thing is bulk upload of multiple objects with multiple files, because of the way multipart data is formed

toxic gate