(hashlips_art_engine-1.1.2_patch_v5)“路径”参数必须是字符串类型。收到未定义
我正在尝试更新一些元数据以获取10个JSON文件,其中一些文件可以正确更新,而另一些则给我以下错误。
下面您可以看到6个文件给我错误,并且正确更新了4个文件。
错误:typeError [err_invalid_arg_type]:“路径”参数必须是类型字符串。未定义的
错误:typeError [err_invalid_arg_type]:“路径”参数必须是类型字符串。未定义的
错误:typeError [err_invalid_arg_type]:“路径”参数必须是类型字符串。未定义的
错误:typeError [err_invalid_arg_type]:“路径”参数必须是类型字符串。未定义的
错误:typeError [err_invalid_arg_type]:“路径”参数必须是类型字符串。未定义的
错误:typeError [err_invalid_arg_type]:“路径”参数必须是类型字符串。收到未定义的
5.png上传& 5. json更新了!
4.png上传& 4. json更新了!
1.png上传& 1. json更新了!
7.png上传& 7. json更新了!
这是node.js代码IM使用:
onst 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: 'SecretApiCode',
},
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));
});
我有10png文件:
1.png 2.png 3.png 4.png 5.png 6.png 7.png 8.png 9.png 10.png
和10个JSON文件
1.JSON 2.Json 3.JSON 4.JSON 5.JSON 6.JSON 7.JSON 8.JSON 9.JSON 10.JSON
I'm trying to update some metadata for 10 JSON files, some of them get updated properly and some others give me the following error.
Below you can see 6 files give me errors and 4 files are correctly updated.
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
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
5.png uploaded & 5.json updated!
4.png uploaded & 4.json updated!
1.png uploaded & 1.json updated!
7.png uploaded & 7.json updated!
This is the node.js code im using:
onst 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: 'SecretApiCode',
},
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));
});
I have 10png files:
1.png
2.png
3.png
4.png
5.png
6.png
7.png
8.png
9.png
10.png
And 10 JSON files
1.json
2.json
3.json
4.json
5.json
6.json
7.json
8.json
9.json
10.json
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中的问题似乎与获取 URL 有关。
事实证明,有时您的代码会因错误而失败,有时则可以成功运行。
可能会发生的情况是,有时,当您从 URL 获取数据时,它可能没有
file_name
属性(由于错误等)。当您尝试使用以下命令解析路径时下面的方法:
file_name
可能是未定义
,因为可能没有这样的键。并且,由于path.parse()
无法解析undefined
,它会返回一个错误,表示 in 只接受string
(如下所示) )。有许多可能的解决方案,因此下面列出了它们。
添加检查
file_name
是否可用。如果不是,则不要尝试使用path.parse()
解析它。您可以使用下面的代码来检查它(在第二个.then()
方法中)。如果您的API支持,请检查响应正文中是否有错误。假设错误类似于
.error
,您可以使用下面的代码作为指导(将其添加到第二个.then()
方法中)。可能还有许多其他(更好)的解决方案,但这些解决方案只是为您提供一个起点。
解决方案 1 和 2 非常相似,但它们在不同的情况下工作。< /子>
The issue in your code seems to be with fetching the URL.
This is proven as sometimes, your code fails with an error, and sometimes, it works successfully.
What might be happening is that sometimes, when you are fetching the data from the URL, it might not have the
file_name
property (because of an error, etc.)When you are trying to parse the path with the below method:
The
file_name
might beundefined
as there might not be such a key. And, aspath.parse()
can't parseundefined
, it returns an error, saying that in only acceptsstring
s (shown below).There are many possible solutions, so they are listed below.
Add a check for if
file_name
is available. If it isn't, then don't try parsing it withpath.parse()
. You can use the code below to check it (in the second.then()
method).If your API supports it, check if there is an error in the response body. Assuming that the error will be something like
.error
, you can use the code below for guidance (add it in the second.then()
method).There may be many other (better) solutions then these, but these solutions are just to give you a starting point.
Solution 1 and 2 are very similar, but they work under different circumstances.
试试这个。如果将标签更改为 v1.2.0,您还可以在 codeSTACKr/create-10k-nft-collection" git 存储库中找到它。
这是在 uploadfiles.js 中
Try this. You can also find it in codeSTACKr/create-10k-nft-collection" git repository if you change tag to v1.2.0.
This is in uploadfiles.js