Following bulk upload article getting error

Hello, I am following :

I have gotten all the code copied over from the images.js section. I’ve added my key and I have all the packages installed.

But when I run node images.js I get the following error:

C:\Users\user\Desktop\IPFS-UPLOAD-BULK\images.js:14
                content: data.toString("base64")
                              ^

TypeError: Cannot read properties of undefined (reading 'toString')        
    at ReadFileContext.callback (C:\Users\user\Desktop\IPFS-UPLOAD-BULK\images.js:14:31)ages.js:14:31)

    at FSReqCallback.readFileAfterOpen [as oncomplete] (node:fs:314:13) 

Can someone please advise what I need to do to resolve this error. I have a feeling its due to my lack of understanding of fs.readFile().

It may be a problem when you don’t have the file that it tries to read on disk
You could try with fewer files, if you use a constant for now many files to process.

Hi, I’m facing the exact same problem, been trying to place my images in the root, in the export folder and in the images folder and the same error persists.

1 Like

Usually this error happened in the past when the images were not present in the folder/folders where the script tries to read from, there was also an error related to size, when the folder has more than 5-10MB, but in that case I think that it is a different error

Thanks for the reply. Where should the source images be stored for this to work? I’ve tried moving them between the folders with no luck and the files I am trying to use are all <1mb each.

Just tried some troubleshooting and I’d like to make a note of the things I’ve tried

  1. The images.js script provided uses ‘.png’ file extensions while the tutorial should work with jpgs. I’ve tried using PNGs and also changing the file extension in the images.js to .jpg to match my images. Both still throw the same error.

  2. I tried this on a folder whose path has no spaces since I know spaces in file paths could lead to some issues. However this also resulted in the same error.

  3. I’ve tried creating folders called “images” and “export” in the root and putting the images in there, I still get the same error.

Yes I have tried these steps as well with no luck. I am glad I am not the only one with this issue.

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}.png`, (err, data) => {
            if(err) rej();
            ipfsArray.push({
                path: `images/${paddedHex}.png`,
                content: data.toString("base64")
            })
            res();
        })
    }))
}

for example in this code it tries to access 100 images, you may not have 100 images locally.

the total size has to be less than ~5MB now (we are working on fixing this limit)

${__dirname}/export/${paddedHex}.png this should be the folder from where it tries to read the images, you could change that location

I’m working with a test folder that only as 2 JPGs in it
The folder structure I am using is:

C:\Users\user\Desktop\IPFS-UPLOAD-BULK\export

The 2 jpgs are in the export folder.

When I change the call to ::

promises.push(new Promise( (res, rej) => {
        fs.readFile(`C:/Users/user/Desktop/IPFS-UPLOAD-BULK/export/0000000000000000000000000000000000000POM00007435-high-resolution.jpg`, (err, data) => {
            if(err) rej();
            ipfsArray.push({
                path: `images/${paddedHex}.png`,
                content: data.toString("base64")
            })
            res();
        })
    }))

I at least get a different error which is ::

C:\Users\user\Desktop\IPFS-UPLOAD-BULK>node images.js
RangeError: Invalid string length
    at stringify (<anonymous>)
    at stringifySafely (C:\Users\user\Desktop\IPFS-UPLOAD-BULK\node_modules\axios\lib\defaults.js:41:37)
    at Object.transformRequest (C:\Users\user\Desktop\IPFS-UPLOAD-BULK\node_modules\axios\lib\defaults.js:76:14)
    at transform (C:\Users\user\Desktop\IPFS-UPLOAD-BULK\node_modules\axios\lib\core\transformData.js:18:15)
    at Object.forEach (C:\Users\user\Desktop\IPFS-UPLOAD-BULK\node_modules\axios\lib\utils.js:245:10)
    at Object.transformData (C:\Users\user\Desktop\IPFS-UPLOAD-BULK\node_modules\axios\lib\core\transformData.js:17:9)
    at dispatchRequest (C:\Users\user\Desktop\IPFS-UPLOAD-BULK\node_modules\axios\lib\core\dispatchRequest.js:35:31)
    at Axios.request (C:\Users\user\Desktop\IPFS-UPLOAD-BULK\node_modules\axios\lib\core\Axios.js:108:15)
    at Axios.<computed> [as post] (C:\Users\user\Desktop\IPFS-UPLOAD-BULK\node_modules\axios\lib\core\Axios.js:140:17)
    at Function.wrap [as post] (C:\Users\user\Desktop\IPFS-UPLOAD-BULK\node_modules\axios\lib\helpers\bind.js:9:15)

Can you tell me if I am getting closer to an answer or if this is just the wrong move all together?

I doesn’t look like a wrong move all together, but I don’t know exactly why do you get that error either

I’m trying that @hello_J is doing by placing an absolute path in images.js. I eventually ended up with another error but it was looking a bit like what @hello_J posted:

SyntaxError: Octal escape sequences are not allowed in template strings.
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1025:15)
    at Module._compile (node:internal/modules/cjs/loader:1059:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

Could this by any chance be caused by a wrong node version? I’m running v17.0.1

Edit: I compared it to @hello_J’s post and i realise i might be looking at something else altogether, sorry.

you could use simple strings for path like “C:\asfadf” in case that you also use that \ unescaped

you can not see that, but I added two of \ there

Thanks, just gave that a shot and I’m seeing some other stuff in my terminal, however now I have a new error

Error [ERR_FR_MAX_BODY_LENGTH_EXCEEDED]: Request body larger than maxBodyLength limit

in the bottom of all that I see

  response: undefined,
  isAxiosError: true,
  toJSON: [Function: toJSON]

The file is just over 800kb, that should be well within the aforementioned 5mb limit, right?

800kb should work, maybe it tries to upload more than you expect it to upload

I have figured out why this doesn’t work.
Our files are NOT named correctly.
We should name the files according to the paddedHex pattern to get this to work as written.
To help with naming you can add the number of files to that you want to go through to the for loop

for (let i = 0; i < 10; i++)
let paddedHex = ("0000000000000000000000000000000000000000000000000000000000000000" + i.toString(16)).substr("-64");
     console.log(paddedHex);

to give you the files names to use, just add your file type at the end for example::

0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000002
0000000000000000000000000000000000000000000000000000000000000003
0000000000000000000000000000000000000000000000000000000000000004
0000000000000000000000000000000000000000000000000000000000000005
0000000000000000000000000000000000000000000000000000000000000006
0000000000000000000000000000000000000000000000000000000000000007
0000000000000000000000000000000000000000000000000000000000000008
0000000000000000000000000000000000000000000000000000000000000009

Also if you’re on Windows the first part should be ::

fs.readFile(`${__dirname}\\export\\${paddedHex}.jpg`, (err, data) => {
        if (err) {
          console.error(err)
          return
        }
        ipfsArray.push({
            path: `images/${paddedHex}.jpg`,
            content: data.toString("base64")
        })
        res();
      })

with \ to escape the “” in the path

2 Likes

I was able to bulk upload but now I am having issues with deploying the smart contract. I am trying to use a for loop to upload 100 images. Here is the error I am getting:

TypeError: Wrong argument count for function call: 2 arguments given but expected 4.
--> token.sol:13:13:
|
13 | _mint(msg.sender, i);
| ^^^^^^^^^^^^^^^^^^^^

Here’s the smart contract:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/ERC1155.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol";

contract NFT is ERC1155 {
    using SafeMath for uint256;

    constructor() ERC1155("ipfs://QmX84qtcq8bRTFwKjAv9kjtztGFjL5PQFHvAvsut1iDcYq/metadata/{id}.json") {
        for (uint i = 0; i < 100; i++) {
            _mint(msg.sender, i); 
        }
    }

}

it looks like _mint function has 4 parameters, for example: _mint(account,id,amount,"");

Perfect, that worked. Thank you!