So I have just come across a very strange bug. I am trying to bulk upload images to IPFS using a private API key. The images are coming from my project directory and when I run the code, it works but only for a couple images. The rest of the images get the same error shown, that I am not using a string when it wants a string. The strangest thing is that when I re-run the code, I get anywhere from 2 images working to 5 working, the rest failing. And in almost every test, image 8 is the first one being uploaded. This is so strange to me, I can't figure out if my code is wrong (but then my errors would be consistent, right?), or maybe my API key is broken somehow, or maybe it's just doing everything too fast for the server to keep up. I am lost, any ideas?
FILE I AM RUNNING:
import FormData from 'form-data';
import fetch from 'node-fetch';
import path from 'path';
const basePath = process.cwd();
import fs from 'fs';
fs.readdirSync(`${basePath}/build/images`).forEach(file => {
const formData = new FormData();
const fileStream = fs.createReadStream(`${basePath}/build/images/${file}`);
formData.append('file', fileStream);
let url = 'https://api.nftport.xyz/v0/files';
let options = {
method: 'POST',
headers: {
Authorization: (private),
},
body: formData
};
fetch(url, options)
.then(res => res.json())
.then(json => {
const fileName = path.parse(json.file_name).name;
let rawdata = fs.readFileSync(`${basePath}/build/json/${fileName}.json`);
let metaData = JSON.parse(rawdata);
metaData.file_url = json.ipfs_url;
fs.writeFileSync(`${basePath}/build/json/${fileName}.json`, JSON.stringify(metaData, null, 2));
console.log(`${json.file_name} uploaded and ${fileName}.json updated!`);
})
.catch(err => console.error('error:' + err));
})
OUTPUT:
DeprecationWarning: form-data doesn't follow the spec and requires special treatment. Use alternative package
(Use `node --trace-deprecation ...` to show where the warning was created)
error:TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
error:TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
error:TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
8.png uploaded and 8.json updated!
10.png uploaded and 10.json updated!