getting error on attaching attachments

Error uploading product: TypeError: Cannot read properties of undefined (reading 'Symbol(Symbol.asyncIterator)') on uploading attachmentbuilder[]
8 Replies
d.js toolkit
d.js toolkit8mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - 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!
Tcukawi
TcukawiOP8mo ago
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.' });
}
};
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.' });
}
};
duck
duck8mo ago
please share the whole error
Tcukawi
TcukawiOP8mo ago
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)
duck
duck8mo ago
sounds like one or more of file.buffer was undefined
Tcukawi
TcukawiOP8mo ago
file.buffer indeed is defined, because on my multer testcase it uploaded the image
duck
duck8mo ago
please actually log file.buffer as you iterate
Tcukawi
TcukawiOP8mo ago
jesus christ, it is undefined
Want results from more Discord servers?
Add your server