IPFS-BULK-UPLOAD project 'toString' error

I am getting the following error. I have reduced the file sizes and am only referencing 3 images in the folder for now. The images are .jpeg format. I changed the file extensions in the script to match. I am using the 64 hex naming convention.
Also, I am seeing that the .substr in i.toString(16)).substr("-64") is depricated but not sure what I should use instead.

Davids-MacBook-Air-3:ipfs-bulk-upload David$ node images.js
/Users/David/Desktop/ipfs-bulk-upload/images.js:15
content: data.toString(“base64”)
^

TypeError: Cannot read property ‘toString’ of undefined
at ReadFileContext.fs.readFile [as callback] (/Users/David/Desktop/ipfs-bulk-upload/images.js:15:31)
at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:441:13)

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

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

for (let i = 0; i < 100; i++) {
    let paddedHex = ("0000000000000000000000000000000000000000000000000000000000000000" + i.toString(16)).substr("-64");

    promises.push(new Promise((res, rej) => {
        fs.readFile(`${__dirname}/export/${paddedHex}.jpeg`, (err, data) => {
            if (err) rej();
            ipfsArray.push({
                path: `images/${paddedHex}.jpeg`,
                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": 'w3gMLAPhijPzdQZE8Y3s8USUSAKzPBdy0yhcavZE7AtfCzWg241kosqB79TP0Q5B',
                "Content-Type": "application/json",
                "accept": "application/json"
            }
        }
    ).then((res) => {
        console.log(res.data);
    })
        .catch((error) => {
            console.log(error)
        })
})

can you use console.log for that path to see if you get the right path there?

seems like the data is undefined, do you really have images that have that paddedHex format under the export folder, because if data returns empty, then it might be the image file doesn’t exists

Here is my file setup:

What is the correct syntax to console.log that path?

I guess that console.log(${__dirname}/export/${paddedHex}.jpeg) before that line

Like this? I get syntax errors
console.log(${__dirname}/export/${paddedHex}.jpeg)
${__dirname}/export/${paddedHex}.jpeg

maybe like that with those ` before and and the end of the text

Here is the result:

${ __dirname } /export/${paddedHex}.jpeg
${ __dirname } /export/${paddedHex}.jpeg
${ __dirname } /export/${paddedHex}.jpeg
${ __dirname } /export/${paddedHex}.jpeg
${ __dirname } /export/${paddedHex}.jpeg
${ __dirname } /export/${paddedHex}.jpeg
${ __dirname } /export/${paddedHex}.jpeg
/Users/David/Desktop/ipfs-upload-bulk/images.js:16
content: data.toString(“base64”)
^

TypeError: Cannot read property ‘toString’ of undefined
at ReadFileContext.fs.readFile [as callback] (/Users/David/Desktop/ipfs-upload-bulk/images.js:16:31)

I would expect to see a path there where __dirname is replace with a path

I ran console.log(__dirname) and it did give the path:

/Users/David/Desktop/ipfs-upload-bulk
/Users/David/Desktop/ipfs-upload-bulk
/Users/David/Desktop/ipfs-upload-bulk
/Users/David/Desktop/ipfs-upload-bulk
/Users/David/Desktop/ipfs-upload-bulk
/Users/David/Desktop/ipfs-upload-bulk
/Users/David/Desktop/ipfs-upload-bulk
/Users/David/Desktop/ipfs-upload-bulk/images.js:16
content: data.toString(“base64”)
^

TypeError: Cannot read property ‘toString’ of undefined
at ReadFileContext.fs.readFile [as callback] (/Users/David/Desktop/ipfs-upload-bulk/images.js:16:31)
at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:441:13)

I just saw on GitHub that two other people have posted this same error as an issue. Might be an issue with the published code.

more people had this problem, and usually was because the files were not in the right folder, you will find on this forum other threads about it

Ok thanks, I am deleting and starting from scratch.

Ok I have solved the issue. But first please note something that could contribute to a file reading issue.
1)
In GitHub and on the name of the file is “IPFS-BULK-UPLOAD” but the path in the files in the tutorial is IPFS-UPLOAD-BULK which could cause some errors and may have been my initial problem. I created my folder based on the GitHub file name not on the path name in the package.json.

Make sure that your first image file ends in 0 not 1. So 0.png not 1.png otherwise it will flag the an error.
0000000000000000000000000000000000000000000000000000000000000000.png
0000000000000000000000000000000000000000000000000000000000000001.png
0000000000000000000000000000000000000000000000000000000000000003.png

One other thing that causes the error:

content: data.toString(“base64”)

Pay attention the number in this line of code:

for (let i = 0; i < 100; i++) {

if you have less than 100 image files then it fails. If you have 100 or more it will run so make sure that you adjust this number accordingly to the number of files you are trying to upload.

1 Like

Hello dr7 I am still getting the same error, I followed all the above process :frowning: I am not a tech guy, I am only following the youtube video now stuck. Please suggest some ideas.
errors as follows:

TypeError: Cannot read properties of undefined (reading ‘toString’)
at ReadFileContext.callback (/Users/thelastmafias/Desktop/ipfs-bulk-upload/images.js:15:31)
at FSReqCallback.readFileAfterOpen [as oncomplete] (node:fs:314:13)
Sharmas-iMac:ipfs-bulk-upload thelastmafias$ node images.js
/Users/thelastmafias/Desktop/ipfs-bulk-upload/images.js:15
content: data.toString(“base64”)

yeah seems so, would be better if you can get the number of images on the folder programmatically, so no need to change every time deleting or adding new images

Hey seems like data variable is undefined, I suggest you to make sure that there is an existing file on your project to be read, maybe since you are non technical guy also suggest have someone technical you know to help you

Can you post your code?