I'm using a form on the client side where the user could upload files, as many as he wants to, and then I send it all over with axios.
Receiving it in NodeJS, I'm using Multer to get the files and its informations with req.files, like that:
[ { fieldname: 'file1',
originalname: 'someimage.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
destination: './uploads/',
filename: '71027041755f49366c132555066d5b89',
path: 'uploads\\71027041755f49366c132555066d5b89',
size: 69336 } ]
As we can see, it's a Array, which could contain more than one file, which is nice, but here comes my problem.
I want to store the informations of those files in MongoDB using Mongoose, so that I know how the file is called, its destination, size, etc.
I defined a Schema in Mongoose like that:
const someSchema = new Schema({
files: {
originalname: String,
filename: String,
mimetype: String,
size: String,
destination: String,
path: String
}
});
But this would only allow me to store one file information. I also tried earlier with:
const someSchema = new Schema({
files: Array
});
But this won't work (TypeError: Cannot set property 'originalname' of undefined). I also tried setting Object, which also errors out (TypeError: Cannot read property '0' of undefined).
By the way, I'm reading the "files" with:
for (let i = 0; i < req.files.length; i++) {
someschema.files[i].originalname = req.files[i].originalname;
someschema.files[i].filename = req.files[i].filename;
someschema.files[i].mimetype = req.files[i].mimetype;
someschema.files[i].size = req.files[i].size;
someschema.files[i].destination = req.files[i].destination;
someschema.files[i].path = req.files[i].path;
}
Logging every req.files[i].XXX above, everything is defined as it should.
Is there a way to make a schema which accepts multiple "files"?
In other words: Is there a way to make a schema which accepts a Array with multiple Objects, if possible with those exact keys?
Thanks in advance.
EDIT: I came up with files: [Object], but this also won't work (TypeError).