#getting error on attaching attachments
8 messages · Page 1 of 1 (latest)
- What's your exact discord.js
npm list discord.jsand nodenode -vversion? - Not a discord.js issue? Check out #1081585952654360687.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!
export const cdnUpload: RequestHandler = async (req, res) => {
if (!req.files || (req.files as Express.Multer.File[]).length < 1) return res.status(500).json({ message: "No image set" });
try {
// Extract product data from the request
const { produk, price, stock, description } = req.body;
console.log(produk);
console.log(price);
console.log(stock);
const images = (req.files as Express.Multer.File[]).map((file) => new AttachmentBuilder(file.buffer).setFile(file.buffer).setName(file.filename)); // Convert uploaded files to MessageAttachment instances
const id = await generateUniqueID();
// Get the target guild
console.log(client)
const guild = await client.guilds.fetch(process.env.GUILD!);
// Create a new channel in the target guild
const channel = await guild.channels.create({
name: `product-${produk}-${id}`,
reason: 'New product upload',
});
// Send the images and product details to the new channel
const productMessage = await channel.send({
content: `**Product Name:** ${produk}\n**Price:** ${price}\n**Stock:** ${stock}\n**Description:** ${description}`,
files: images,
});
// Create a new product document
const newProduct = new ProductModel({
id,
nama: produk,
price,
stok: stock,
description,
img: productMessage.attachments.map((attachment:Attachment) => attachment.url),
});
// Save the product to the database
await newProduct.save();
// Respond with success
res.status(200).json({ message: 'Product uploaded successfully!' });
} catch (error) {
console.error('Error uploading product:', error);
res.status(500).json({ message: 'Error uploading product.' });
}
};
please share the whole error
Error uploading product: TypeError: Cannot read properties of undefined (reading 'Symbol(Symbol.asyncIterator)')
at DataResolver.resolveFile (/Users/farrel/Desktop/Code/project-website-fire_fighter/node_modules/discord.js/src/util/DataResolver.js:117:24)
at MessagePayload.resolveFile (/Users/farrel/Desktop/Code/project-website-fire_fighter/node_modules/discord.js/src/structures/MessagePayload.js:260:54)
at /Users/farrel/Desktop/Code/project-website-fire_fighter/node_modules/discord.js/src/structures/MessagePayload.js:225:85
at Array.map (<anonymous>)
at MessagePayload.resolveFiles (/Users/farrel/Desktop/Code/project-website-fire_fighter/node_modules/discord.js/src/structures/MessagePayload.js:225:56)
at TextChannel.send (/Users/farrel/Desktop/Code/project-website-fire_fighter/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:154:50)
at cdnUpload (file:///Users/farrel/Desktop/Code/project-website-fire_fighter/dist/controller/api/cdnUpload.js:36:46)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
sounds like one or more of file.buffer was undefined
file.buffer indeed is defined, because on my multer testcase it uploaded the image
please actually log file.buffer as you iterate