如何使用 NodeJS 遍历 JSON 文件
How to loop through JSON file with NodeJS
我正在为高级会员开发一个带有命令的 Discord 机器人。
buyers.json:
buyers.json:
{
"buyers": [
{
"id": "331499609509724162"
},
{
"id": "181336616164392960"
},
{
"id": "266389854122672128"
}
]
}
代码片段:
case "spotify":
var uID = message.author.id;
for (let i = 0; i < ftpr.buyers.length; i++) {
if (uID === ftpr.buyers[i]) {
var _embed = new Discord.RichEmbed()
.setTitle("Spotify")
.addField("Username", "testsda@yahoo.com")
.addField("Password", "ithastobe8")
.setColor(0x00FFFF)
.setFooter("Sincerely, LajgaardMoneyService")
.setThumbnail(message.author.avatarURL);
message.author.send(_embed);
console.log(message.author + " Just used the command !spotify")
}
}
break;
除 for 循环和 if 语句外,一切正常。好像没拿到id。
var fptr = require("./buyers.json");
编辑:
我的 json 文件如下所示:
{
"buyers": [
{
"id": "331499609509724162"
},
{
"id": "181336616164392960"
},
{
"id": "266389854122672128"
}
]
}
它有一些来自人们的 ID。
在我的app.js是用var file = require("./file.json");
导入的。假设我更改了其中一个 ID,然后我必须重新加载 app.js 才能更新它。如果我不这样做,我的检查机制就没有使用 file.json
的更新版本
每次使用函数时我应该如何更新文件?
您正在比较 uID,我假设它是一个字符串,与数组中的每个对象(包含 一个字符串) .
尝试与买家对象的 id 属性进行比较:
if (uID === ftpr.buyers[i].id) { }
确保 message.author.id
和 ftpr.buyers id 的类型相同,ftpr.buyers
是包含字符串 id 的对象数组。你应该 console.log message.author.id
的类型 console.log(typeof message.author.id,message.author.id)
您可以通过在 switch case 之前设置一个包含 id 的字符串数组来简化代码:
const ids = ftpr.buyers.map(b = b.id);//this is now ["331499609509724162",...]
case "spotify":
if (ids.includes(message.author.id)) {
var _embed = new Discord.RichEmbed()
.setTitle("Spotify")
.addField("Username", "testsda@yahoo.com")
.addField("Password", "ithastobe8")
.setColor(0x00FFFF)
.setFooter("Sincerely, LajgaardMoneyService")
.setThumbnail(message.author.avatarURL);
message.author.send(_embed);
console.log(message.author + " Just used the command !spotify")
}
break;
我正在为高级会员开发一个带有命令的 Discord 机器人。
buyers.json:
buyers.json:
{
"buyers": [
{
"id": "331499609509724162"
},
{
"id": "181336616164392960"
},
{
"id": "266389854122672128"
}
]
}
代码片段:
case "spotify":
var uID = message.author.id;
for (let i = 0; i < ftpr.buyers.length; i++) {
if (uID === ftpr.buyers[i]) {
var _embed = new Discord.RichEmbed()
.setTitle("Spotify")
.addField("Username", "testsda@yahoo.com")
.addField("Password", "ithastobe8")
.setColor(0x00FFFF)
.setFooter("Sincerely, LajgaardMoneyService")
.setThumbnail(message.author.avatarURL);
message.author.send(_embed);
console.log(message.author + " Just used the command !spotify")
}
}
break;
除 for 循环和 if 语句外,一切正常。好像没拿到id。
var fptr = require("./buyers.json");
编辑: 我的 json 文件如下所示:
{
"buyers": [
{
"id": "331499609509724162"
},
{
"id": "181336616164392960"
},
{
"id": "266389854122672128"
}
]
}
它有一些来自人们的 ID。
在我的app.js是用var file = require("./file.json");
导入的。假设我更改了其中一个 ID,然后我必须重新加载 app.js 才能更新它。如果我不这样做,我的检查机制就没有使用 file.json
的更新版本
每次使用函数时我应该如何更新文件?
您正在比较 uID,我假设它是一个字符串,与数组中的每个对象(包含 一个字符串) .
尝试与买家对象的 id 属性进行比较:
if (uID === ftpr.buyers[i].id) { }
确保 message.author.id
和 ftpr.buyers id 的类型相同,ftpr.buyers
是包含字符串 id 的对象数组。你应该 console.log message.author.id
的类型 console.log(typeof message.author.id,message.author.id)
您可以通过在 switch case 之前设置一个包含 id 的字符串数组来简化代码:
const ids = ftpr.buyers.map(b = b.id);//this is now ["331499609509724162",...]
case "spotify":
if (ids.includes(message.author.id)) {
var _embed = new Discord.RichEmbed()
.setTitle("Spotify")
.addField("Username", "testsda@yahoo.com")
.addField("Password", "ithastobe8")
.setColor(0x00FFFF)
.setFooter("Sincerely, LajgaardMoneyService")
.setThumbnail(message.author.avatarURL);
message.author.send(_embed);
console.log(message.author + " Just used the command !spotify")
}
break;