2

I'm trying to parse so I can check if the username is valid or not. Though I have little to no experience working with JSON parasing in NodeJS. I'd appreciate some help on this issue. This has been a struggle moving over to NodeJS and trying to work with APIs and parasing them.

Here's the code and here is the error

 userid = body[0]['data']['user']['id'];
                        ^

TypeError: Cannot read properties of undefined (reading 'data')
    at Request._callback (C:\Users\Tommy\Desktop\Misc\Node Projects\Discord Twitch Username Check\index.js:141:25)
    at Request.self.callback (C:\Users\Tommy\Desktop\Misc\Node Projects\Discord Twitch Username Check\node_modules\request\request.js:185:22)
    at Request.emit (node:events:390:28)
    at Request.<anonymous> (C:\Users\Tommy\Desktop\Misc\Node Projects\Discord Twitch Username Check\node_modules\request\request.js:1154:10)
    at Request.emit (node:events:390:28)
    at IncomingMessage.<anonymous> (C:\Users\Tommy\Desktop\Misc\Node Projects\Discord Twitch Username Check\node_modules\request\request.js:1076:12)
    at Object.onceWrapper (node:events:509:28)
    at IncomingMessage.emit (node:events:402:35)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)
function getUser(username) {

    const opts = {
        "url": `https://gql.twitch.tv/gql`,
        headers: {
            'Connection': 'keep-alive',
            'Pragma': 'no-cache',
            'Cache-Control': 'no-cache',
            'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="98", "Google Chrome";v="98"',
            'Accept-Language': 'en-US',
            'sec-ch-ua-mobile': '?0',
            'Client-Version': '7b9843d8-1916-4c86-aeb3-7850e2896464',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',
            'Content-Type': 'text/plain;charset=UTF-8',
            'Client-Session-Id': '51789c1a5bf92c65',
            'Client-Id': 'kimne78kx3ncx6brgo4mv6wki5h1ko',
            'X-Device-Id': 'xH9DusxeZ5JEV7wvmL8ODHLkDcg08Hgr',
            'sec-ch-ua-platform': '"Windows"',
            'Accept': '*/*',
            'Origin': 'https://www.twitch.tv',
            'Sec-Fetch-Site': 'same-site',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Dest': 'empty',
            'Referer': 'https://www.twitch.tv/',
        },
        body: '[{"operationName": "WatchTrackQuery","variables": {"channelLogin": "'+username+'","videoID": null,"hasVideoID": false},"extensions": {"persistedQuery": {"version": 1,"sha256Hash": "38bbbbd9ae2e0150f335e208b05cf09978e542b464a78c2d4952673cd02ea42b"}}}]'
    
    }

    request(opts, (err, res, body) => {
        body = JSON.parse(body);
        userid = body[0]['data']['user']['id'];
        if(userid === 0){
            return "Invalid Username"
        }else{
            return userid;
        }
    });
};
R3D
  • 29
  • 2
  • Can you log the `body` first? – 新Acesyyy Jun 30 '22 at 04:55
  • So, I'm converting my bot from Python to NodeJS. Works fine in Python but in my opinion NodeJS is much faster. http://partycrew.xyz/images/wWFg8ucZFJ_June_30_2022.png – R3D Jun 30 '22 at 04:58
  • is that the log of the `body`? try to log `body[0].data` – 新Acesyyy Jun 30 '22 at 05:35
  • I tried to copy the data on your `array` so It will be `userID[0].data.followUser` but since `followUser` is null, expect that your output is `null` – 新Acesyyy Jun 30 '22 at 05:39
  • I have fixed my issue. It is a post method not a get method. Now just running into error checking whether it is null or an actual user. – R3D Jun 30 '22 at 06:23
  • Yes, `POST` for putting `data` on your database then `GET` to get `data` on your database. – 新Acesyyy Jun 30 '22 at 06:58
  • Now I'm running into where when I add checks if user returns null. `body = JSON.parse(body); user = body[0]['data']['user']; if(user != null){ userid = body[0]['data']['user']['id']; console.log(userid); return userid['id']; }else{ console.log(body); return null; }` Either way if it invalid or a valid user it still returns this message for either one `if(getUser(args[0]) == null){ return "Invalid User" }` – R3D Jun 30 '22 at 07:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/246051/discussion-between-r3d-and-acesyyy). – R3D Jun 30 '22 at 07:04

0 Answers0