Я получаю эту ошибку при попытке опубликовать изображение вместе с данными json. Ошибка: значение аргумента «данные» не является допустимым документом Firestore. Невозможно использовать "undefined" в качестве значения Firestore (находится в поле "цена") введите описание изображения здесь
let imageUrl;
const BusBoy = require('busboy');
const path = require('path');
const os = require('os');
const fs = require('fs');
const busboy = new BusBoy({ headers: req.headers });
let imageToBeUploaded = {};
let imageFileName;
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
console.log(fieldname, file, filename, encoding, mimetype);
if (mimetype !== 'image/jpeg' && mimetype !== 'image/png') {
return res.status(400).json({ error: 'Wrong file type submitted' });
}
// my.image.png => ['my', 'image', 'png']
const imageExtension = filename.split('.')[filename.split('.').length - 1];
// 32756238461724837.png
imageFileName = `${Math.round(
Math.random() * 1000000000000
).toString()}.${imageExtension}`;
const filepath = path.join(os.tmpdir(), imageFileName);
imageToBeUploaded = { filepath, mimetype };
file.pipe(fs.createWriteStream(filepath));
});
busboy.on('finish', () => {
admin
.storage()
.bucket(`${config.storageBucket}`)
.upload(imageToBeUploaded.filepath, {
resumable: false,
metadata: {
metadata: {
contentType: imageToBeUploaded.mimetype
}
}
})
.then(() => {
imageUrl = `https://firebasestorage.googleapis.com/v0/b/${
config.storageBucket
}/o/${imageFileName}?alt=media`;
const newProduct = {
description: req.body.description,
price: req.body.price,
title: req.body.title,
createdAt: new Date().toISOString(),
likeCount:0,
reviewCount:0,
orderCount: 0,
storeName: req.user.userName,
sellerImage: req.user.imageUrl,
imageUrl: imageUrl,
};
db .collection('products')
.add(newProduct)
.then(doc => {
const resProduct = newProduct;
resProduct.productId = doc.id;
res.json(resProduct);
})
})
.then(() => {
return res.json({ message: 'product uploaded successfully' });
})
.catch((err) => {
console.error(err);
return res.status(500).json({ error: 'something went wrong' });
});
});
busboy.end(req.rawBody);
};
Можно ли отправить файл и json данные вместе в почтальоне?