![](/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.