Issue with getting POST from stream webhook with a specific user id


#1

Hello there,

I am having an issue with the discord bot I am making.

What I am using:
nodejs
axios
express
twitch api

I am building a custom discord bot for a friend that will tell when a streamer goes live. I was testing my creation, and was able to get my notification anytime my personal stream went live, we tested with his twitch, and nothing. We then had a friend each go live, and both went through.

after the second or third time we were able to get the error output for when he went live:

TypeError: Cannot read property 'user_id' of undefined
at app.post (/var/www/html/discordCB/DiscordChatBotTemplate/app.js:59:29)

which pertains to these lines of code with the last being the error:

app.post('/userLive', jsonParser, (req, res) => {  
//body is the parameters that are being sent.
let body = req.body;
console.log(body);
let data = body.data[0];
    
twitch.getUserData(data.user_id, data.game_id, function(user, game){

we have tested 4 twitch accounts in total, the only difference that I see is that his USERNAME has an underscore. But, even then, we do not use the username we use the USER_ID.

I have used a chrome extension and put his user id in to it and gotten back his username, and vise versa. I have used this script:

async function getUserId(username){
let userid = await axios.get('https://api.twitch.tv/helix/users?login='+username);
return userid.data.data[0].id;
}

to get back both ID and username.

Any Idea of what it could be?


#2

you should console.log out the whole data object.

As that’ll provide the information as to what is on.

There is not enough information here to help you, for if it’s actually a problem with the user and not your code

But it looks like you are getting and parsing and tripping on a Stream down notification. As let data = body.data[0] will be undefined.

Stream down returns

{“data”:[]}

And you have no data length check, so

let data = body.data[0];

should be

if (body.data.length == 1) {
    // process as stream up
} else {
    // process as stream down
}

Edit: also tweaked your topic to mention you are referring to Webhooks, and not paid subscriptions.


#3

I was previously console.logging the whole req.data that express gives you, I will console.log the whole req.

I am getting this at the end of the error I am posting, so that would make sense.

Thank you, I will add a length check for data. Also thank you for tweaking my topic. I will make some changes and let you know what I am getting back.


#4

So, I just went through everything I got back from the request sent to my server when he went live.

Everything apart from one things(not including timeout numbers and all of that) were weird.

in the headers, the portion for link has MY user id in it when he goes live.

 link:
  '<https://api.twitch.tv/helix/webhooks/hub>; rel="hub", <https://api.twitch.tv/helix/streams?user_id=74030148>; rel="self"',

do you want me to post the whole request here? It is rather long.


#5

Then you got a notification for https://api.twitch.tv/helix/streams?user_id=74030148

And by whole data object it is meant the JSON data sent to you

So it would jsut be a single stream object identical to what is obtained if you call https://api.twitch.tv/helix/streams?user_id=74030148 directly instead of waiting for the webhook