Search code examples
javascriptmongodbexpressmongoosegridfs-stream

Can not access objects key value pairs


I am sending images via FormData from my VueJS FrontEnd to my Express Server to store them in a Mongo DB with gridfs-stream and mongoose. The image objects arrive to the server but I don´t know how I access its key-value pairs.

When I ran

let part = req.files
for (const prop in part) {
   console.log('part.${prop} = ${part[prop]}')
}

I got this output:

part.files[0] = [object Object]
part.files[1] = [object Object]

but when I tried to access part.files[0] I got this error:

TypeError: Cannot read property '0' of undefined

Here is how the Object is structured:

{ 'files[0]':
 { data: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 48 00 48 00 00 ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f ... >,
 name: 'e0ZnCwP.jpg',
 encoding: '7bit',
 mimetype: 'image/jpeg',
 truncated: false,
 size: 259454 },
'files[1]':
 { data: <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 48 00 48 00 00 ff c0 00 11 08 06 40 04 c8 03 01 22 00 02 11 01 03 11 01 ff c4 00 1f 00 00 01 05 01 01 01 ... >,
 name: 'Me.jpeg',
 encoding: '7bit',
 mimetype: 'image/jpeg',
 truncated: false,
 size: 108021 } }

Thanks in advance!


Solution

  • object.files[0] reads the first item of the property called files, which is assumed to be an array. Your object doesn't have this property, is has string keys files[0], files[1] etc. This is probably a mistake, if it is not, you can refer to these props indirectly as object["files[0]"], object["files[1]"], object["files[" + index + "]"] etc