![](/img/trans.png)
[英]TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined and code: 'ERR_INVALID_ARG_TYPE'
[英]error:TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined when trying node utils/nftport/uploadFiles.js
我在尝试这个命令时遇到了这个错误:node utils/nftport/uploadFiles.js 我是这个论坛的新手。
这是uploadFiles.js 代码:
const FormData = require('form-data');
const fetch = require("node-fetch");
const path = require("path");
const basePath = process.cwd();
const fs = require("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: "bla bla ",
},
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 & ${fileName}.json updated!`);
})
.catch((err) => console.error("error:" + err));
});
package.json文件
{
"name": "hashlips_art_engine",
"version": "1.1.1",
"description": "HashLips Art Engine is a tool used to create multiple different instances of artworks based on provided layers.",
"main": "index.js",
"bin": "index.js",
"pkg": {
"assets": [
"layers/**/*",
"node_modules/**/*",
"src/**/*"
]
},
"scripts": {
"build": "node index.js",
"generate": "node index.js",
"rarity": "node utils/rarity.js",
"preview": "node utils/preview.js",
"pixelate": "node utils/pixelate.js",
"update_info": "node utils/update_info.js",
"preview_gif": "node utils/preview_gif.js",
"generate_metadata": "node utils/generate_metadata.js"
},
"author": "Daniel Eugene Botha (HashLips)",
"license": "MIT",
"dependencies": {
"canvas": "^2.8.0",
"cucumber": "^6.0.5",
"form-data": "^4.0.0",
"gif-encoder-2": "^1.0.5",
"react-dev-utils": "^12.0.0",
"react-scripts": "^5.0.0",
"sha1": "^1.1.1"
},
"devDependencies": {
"@types/node-fetch": "^2.0.3"
}
}
根据您在评论中提供的新信息:
由于您的代码的结构方式,您将快速 go 进入一个打开 1000 个读取流并同时启动 1000 个fetch()
请求的循环。 因此,由于这些是非阻塞操作,所有 1000 个操作将同时进行。
这可能会以多种可能的方式导致问题:
所有这些问题都可以通过一次只运行少量请求而不是一次运行全部 1000 个请求来减少,其中 small 是 3-10 范围内的数字。 您可以通过一次只运行一个请求来测试该理论。
您可以通过一次运行一个文件来测试这个理论:
const FormData = require('form-data');
const fetch = require("node-fetch");
const path = require("path");
const basePath = process.cwd();
const fs = require("fs");
async function run() {
const delayBetweenImages = 500;
function delay(t) {
return new Promise(resolve => setTimeout(resolve, t));
}
const files = fs.readdirSync(`${basePath}/build/images`);
for (const file of files) {
const formData = new FormData();
const fullPath = `${basePath}/build/images/${file}`;
const fileStream = fs.createReadStream(fullPath);
// log any read errors
fileStream.on('error', err => {
console.log(`Error reading ${fullPath}`, err);
});
formData.append("file", fileStream);
let url = "https://api.nftport.xyz/v0/files";
let options = {
method: "POST",
headers: {
Authorization: "bla bla ",
},
body: formData
};
await fetch(url, options)
.then((res) => res.json())
.then((json) => {
console.log(`parsing ${json.file_name}`);
const fileName = path.parse(json.file_name).name;
console.log(` filename = ${fileName}`);
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 & ${fileName}.json updated!`);
}).catch((err) => console.error("error:", err));
// you can experiment with how long this delay is and/or
// commenting it out entirely as it may not be needed
await delay(delayBetweenImages);
}
}
run().then(result => {
console.log("done");
}).catch(err => {
console.log(err);
});
变更摘要:
仍然可能存在速率限制问题,您可以通过向循环添加延迟来测试(以减慢请求速度)。
如果您发现减慢速度或限制同时请求的数量确实使其可靠,那么您可以通过一次运行多个(但远少于 1000 个)请求来提高性能。
可以帮助您执行此操作的几个函数mapConcurrent()
(控制一次运行的请求数)和rateLimitMap()
(限制每秒的请求数)可以为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.