每个人我都想循环Nodejs上强大的库给定的文件数组,这是post函数:
form.parse(req, (err, fields, files) => {
console.log(files);
let filesPaths = [];
files.forEach(file => {
filesPaths.push(file.path)
});
res.json({
filesPaths
})
})
,我只是关注这个Youtube Tutorial,但是奇怪的是,它没有像视频中的那样工作,尽管出现了Cannot read property 'forEach' of undefined
的结果,但我还是遇到了console.log(files)
错误。
这是console.log(files)
的日志:
{ test:
File {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
size: 57417,
path: 'uploads\\upload_ecc62d10fe1c5f0c60786125211a95eb.jpg',
name: 'products_364475.jpg',
type: 'image/jpeg',
hash: null,
lastModifiedDate: 2019-05-28T00:03:31.636Z,
_writeStream:
WriteStream {
_writableState: [WritableState],
writable: false,
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
path: 'uploads\\upload_ecc62d10fe1c5f0c60786125211a95eb.jpg',
fd: null,
flags: 'w',
mode: 438,
start: undefined,
autoClose: true,
pos: undefined,
bytesWritten: 57417,
closed: false } },
test2:
File {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
size: 11296,
path: 'uploads\\upload_15de2858098a654c11ac08400b0c18d8.jpg',
name: 'download.jpg',
type: 'image/jpeg',
hash: null,
lastModifiedDate: 2019-05-28T00:03:31.637Z,
_writeStream:
WriteStream {
_writableState: [WritableState],
writable: false,
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
path: 'uploads\\upload_15de2858098a654c11ac08400b0c18d8.jpg',
fd: null,
flags: 'w',
mode: 438,
start: undefined,
autoClose: true,
pos: undefined,
bytesWritten: 11296,
closed: false } } }
答案 0 :(得分:0)
这是我解析和遍历files
的方式:
let filesArray = [];
let json = JSON.parse(JSON.stringify(files));
let key = Object.keys(json);
json[key].forEach(j => {
filesArray.push(j.path.split("\\")[1]);
});
res.json({
filesArray
})