IPFS upload with uploadFolder -> data undefined

Hi , I’m trying to bulk upload NFTs Using IPFS folders (following this code https://github.com/MoralisWeb3/youtub…) and get a content:
data.toString(“base64”) > TypeError: Cannot read property ‘toString’ of undefined. Any idea why that might be?

the code:

let fs = require("fs");
let axios = require("axios");

let ipfsArray = [];
let promises = [];

for (let i = 1; i<270; i++){
    promises.push(new Promise( (res, rej) => {
        fs.readFile(`${__dirname}/export/${i}.png`, (err, data) => {
            if(err) rej();
            ipfsArray.push({
                path: `images/${i}.png`,
                content: data.toString("base64")
            })
            res();
        })
    }))
}
Promise.all(promises).then( () => {
    axios.post("https://deep-index.moralis.io/api/v2/ipfs/uploadFolder", 
        ipfsArray,
        {
            headers: {
                "X-API-KEY": '',
                "Content-Type": "application/json",
                "accept": "application/json"
            }
        }
    ).then( (res) => {
        console.log(res.data);
    })
    .catch ( (error) => {
        console.log(error)
    })
})
1 Like

Hi, you can read here about how to post code in forum: READ BEFORE POSTING - How to post code in the forum

1 Like

it looks like you don’t have this line: https://github.com/MoralisWeb3/youtube-tutorials/blob/0048ae5196051f7596550d52630838654e9bf7a6/ipfs-bulk-upload/images.js#L15

            ipfsArray.push({
                path: `images/${paddedHex}.png`,
                content: data.toString("base64")
            })

did you change it intentionally?

content: data.toString("base64") that was there all the time , that one throws an issue,

path: images/${paddedHex}.png -> the paddedHex i changed to {i} since i have my jpegs numbered from 1-270 , & not padded to 64 hex characters

I see this in the code that you pasted

accidentally deleted it when copy pasted here. retried once again with content: data.toString("base64") -> same results🤔

ok, so the error should be from that line, somehow data variable is undefined there

I am getting the same message on my test attempt, with my path pointing to the correct “unminted” folder in the same location.

Let me know if you maybe will find solution

maybe you try to read more files than you have in your folder, here it tries to read close to 270 files.
You could print file path before trying to read it, and when that data is undefined.
You could also skip ipfsArray.push when data is undefined.

Tried with less, still failing. Do you know maybe how the code will look like if i skip ipfsArray. push? what would be the other way to upload 270 images to ipfs , if not pushing them in array?

I could make an equivalent code in Python if you want, I’m not expert in js syntax, but it should be a simple if there.
You could also decrease that number to 10 instead of 270, and make sure that you have 10 images in your folder

it does work with 10 :thinking:

mine still did not work with 10

if you try with 2 or 3 it works?

no, same error happens even with 1.

and you have those image files in that directory like /export/1.png, /export/2.png?
what is the size of those two .png files in your case?

yes, obviously changing the name to the actual name of the folder in the same directory of the images I want to do this process under. They are between 60-65 kb each

In my case /unminted/1.png, /unminted/2.png, etc

and did you also changed in this line instead of export to unminted?