简体   繁体   English

.JSON文件中如何查看用户id是否存在

[英]How to check if the user id exists in the .JSON file

I'm trying to find ID from the.JSON file so I can do a match if statement, current problem is that users are saved in.JSON each time, not just once.我正在尝试从 .JSON 文件中查找 ID,以便我可以匹配 if 语句,当前的问题是用户每次都保存在 .JSON 中,而不是一次。 So I was looking for a way to find User's ID in the.JSON and then compare it with the statement, but I'm unable to get just the ID from the.JSON(check if the ID exists in that file).所以我一直在寻找一种方法来在.JSON 中找到用户 ID,然后将其与语句进行比较,但我无法仅从 .JSON 中获取 ID(检查该文件中是否存在 ID)。

empty.JSON空.JSON

{
    "users": [
    ]
}

.JSON with collected users .JSON 与收集的用户

{
    "users": [
        {
            "id": "1",
            "username": "USER1"
        },
        {
            "id": "1",
            "username": "USER1"
        },
        {
            "id": "2",
            "username": "USER2"
        },
        {
            "id": "2",
            "username": "USER2"
        }
    ]
}

EDIT编辑

Code:代码:

let rawuserstats = fs.readFileSync(__dirname + '/users.json', 'utf8');
const userstats = JSON.parse(rawuserstats);
console.log(userstats)


//something above...
const foundUser = userstats.users.find(m => m.id === message.author.id)
console.log('Found user:', foundUser)
if (foundUser !== message.author.id) { //try to match user ID with the .JSON(not working)
//Put user's data to the .JSON 
userstats.users.push({id: message.author.id, username: message.author.username});
fs.writeFileSync(__dirname + '/users.json', JSON.stringify(userstats, 0, 4), 'utf8');
}
//something else...

console.log('Found user:', foundUser) returns undefined if.JSON is empty, then after users are in the.JSON { id: '1', username: 'USER1' } , even if I used USER2 for the command instead. console.log('Found user:', foundUser)返回undefined如果.JSON 为空,那么在用户在.JSON { id: '1', username: 'USER1' }之后,即使我使用 USER2 作为命令反而。 Didn't figure out how to get just the ID so I can compare it...没有弄清楚如何只获取 ID 所以我可以比较它......

I tried const foundUser = userstats.users.find(id => id === message.author.id) but this will always return undefined while using console.log('Found user:', foundUser) .我试过const foundUser = userstats.users.find(id => id === message.author.id)但这在使用console.log('Found user:', foundUser)时总是会返回undefined I'm a bit confused about it.我对此有点困惑。

EDIT编辑

I made some progress const foundUser = userstats.users.find(m => m.id === message.author.id) this will return undefined when the user is not inside of the.JSON, and returns this response when user is in the.JSON { id: '1', username: 'USER1' } or { id: '2', username: 'USER2' } from our example.我取得了一些进展const foundUser = userstats.users.find(m => m.id === message.author.id)这将在用户不在.JSON 内部时返回未定义,并在用户在时返回此响应在我们的示例中的.JSON { id: '1', username: 'USER1' }{ id: '2', username: 'USER2' }中。 I just need pick the ID value.我只需要选择 ID 值。

I found a solution, so posting it right here.我找到了一个解决方案,所以把它贴在这里。

    const foundUser = userstats.users.find(m => m.id === message.author.id)
    console.log('Found user:', foundUser)
    if (!foundUser) {
    //Put user's data in the .JSON 
    userstats.users.push({id: message.author.id, username: message.author.username});
    fs.writeFileSync(__dirname + '/users.json', JSON.stringify(userstats, 0, 4), 'utf8');
    }

As foundUser returns undefined, I was able to use this kind of statement and it works, users are not duplicating in.JSON file anymore.当 foundUser 返回未定义时,我能够使用这种语句并且它有效,用户不再复制 in.JSON 文件。

But I would still like to know, how I can get just the ID response (in this case 1 ) instead of { id: '1', username: 'USER1' } , is that possible via .find ?但我仍然想知道,我如何才能获得 ID 响应(在本例中为1 )而不是{ id: '1', username: 'USER1' } ,是否可以通过.find

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM