Я не знаю почему, но моя функция вызывается дважды в storage.single
.
app.post("/upload-file", async (req, res, next) => {
try {
export const inMemoryStorage = (opts = {}) => {
return multer({ storage: multer.memoryStorage(), ...defaultOpts,
...opts })
}
const storage = inMemoryStorage();
storage.single('file')(req, res, () => {
console.log(req.file) // in my last update
console.log('called') // this is returned two times
return this.uploadService.uploadFile(req)
})
} catch (err) {
next(err);
}
});
}
Спасибо, если у вас есть решение. Я просто хочу, чтобы моя функция вызывалась один раз.
---- Новое редактирование ----
Итак, мой uploadService такой:
async uploadFile(req) {
const b2 = new B2({
applicationKeyId: 'private1',
applicationKey: 'private2'
})
const upload = await b2.getUploadUrl({ bucketId: 'my-private-key' })
const myfile = await b2.uploadFile({
uploadUrl: upload.data.uploadUrl,
uploadAuthToken: upload.data.authorizationToken,
fileName: getFilename(req.file),
data: req.file.buffer,
onUploadProgress: (event) => null
})
console.log(myfile) // it works
return myfile
}
Так что мой загрузить работу, и я вижу мой console.log. Таким образом, в моей базе данных, примерно через 15 секунд после первой загрузки, из моего метода отправляется другой файл, и мой console.log возвращает мне дважды objets
---------- Последнее обновление ---- ------
Я заметил кое-что, я не знаю почему, но когда мой файл больше 50 МБ. Моя функция вызывается 2 раза. А под 50мб все правильно и я отправляю только один раз мой файл
Как видите, я добавил console.log (req.file). И результат:
{
fieldname: 'file',
originalname: 'myMovie.mp4',
encoding: '7bit',
mimetype: 'video/mp4',
buffer: <Buffer 1a 45 df a3 01 00 00 00 00 00 00 23 42 86 81 01 42 f7 81 01 42 f2 81 04 42 f3 81 08 42 82 88 6d 61 74 72 6f 73 6b 61 42 87 81 04 42 85 81 02 18 53 80 ... 107390076 more bytes>,
size: 107390126
}
{
fieldname: 'file',
originalname: 'myMovie.mp4',
encoding: '7bit',
mimetype: 'video/mp4',
buffer: <Buffer 1a 45 df a3 01 00 00 00 00 00 00 23 42 86 81 01 42 f7 81 01 42 f2 81 04 42 f3 81 08 42 82 88 6d 61 74 72 6f 73 6b 61 42 87 81 04 42 85 81 02 18 53 80 ... 107390076 more bytes>,
size: 107390126
}
Так что мои два запроса одинаковы