Stateford / twitch-api

Easy node interaction with the twitch API, using promises
https://www.npmjs.com/package/twitch.tv-api
6 stars 7 forks source link

Unexpected token < in JSON at position 0 #14

Closed RisedSky closed 4 years ago

RisedSky commented 5 years ago

image

Stateford commented 5 years ago

Do you have any more information?

What version of node are you using? Which api method are you calling? Do none of them work?

RisedSky commented 5 years ago

In fact it works also it happens randomly, maybe because of a lot of call ? Also, here is my versions

Node version v11.4.0 NPM version 6.4.1 GetUser call

Stateford commented 5 years ago

are you catching the error? I believe GetUser might throw if the user is offline.

RisedSky commented 5 years ago

image Yes i'm catching the error to prevent the bot to crash ^^

RisedSky commented 5 years ago

Also, i'm checking if the user isn't streaming : image

Also i have this error too : image

image

RisedSky commented 5 years ago

Also, do you know where the problem can be ?

Stateford commented 5 years ago

Can you show me where you call the API that's failing.

RisedSky commented 5 years ago

It doesn't seem to say where the problem can be :/

Stateford commented 5 years ago

I would try running it through a debugger and seeing what value the API is returning before attempting to JSON.parse.

I think if the user is offline it should fail and you need to .catch it, but it's possible a dependency has broken.

RisedSky commented 4 years ago

Hey, i'm back here (1 year later lol) image I've got this issue here also, i use this code :

bot.twitch.getUser(Streaming_User.UserTwitch)
                //bot.twitch.getUser("squeezielive")
                .then(async data => {
                    const dataUser = await data;
                    /* *
                    _id: 33166101328
                    average_fps: 60
                    broadcast_platform: "live"
                    channel: Object { mature: false, status: "Squeezie ► La squad la plus éclatée du jeu (ft Loc…", broadcaster_language: "fr", … }
                    status:"Squeezie ► La squad la plus éclatée du jeu (ft Locklear) #sponsorisé"
                    updated_at:"2019-03-12T20:22:40.323794Z"
                    url:"https://www.twitch.tv/squeezielive"
                    video_banner:"https://static-cdn.jtvnw.net/jtv_user_pictures/1e3c5fd7-de47-4a59-bc94-50baa1572123-channel_offline_image-1920x1080.png"
                    views: 11 554 222
                    community_id: ""
                    community_ids: Array(0)[]
                    created_at: "2019-03-12T18:55:38Z"
                    delay: 0
                    game: "Tom Clancy's The Division 2"
                    is_playlist: false
                    stream_type:"live"
                    video_height:1080
                    viewers:8653
                    * */

                    //console.log(dataUser)
                    if (dataUser.stream == null || dataUser.stream == undefined) {
                        //console.log(colors.green(`dataUser=NULL`))
                        return Delete_User_data_and_Streaming_Status(Streaming_User)
                    } else {
                        //console.log(colors.green(`dataUser!=NULL`))
                        //console.log(colors.green("il devrait stream normalement !"))
                        //console.log(colors.green(dataUser.stream.channel.name))

                        var guild_user = bot.bot.guilds.find(g => g.id == Streaming_User.ServerID)
                        if (!guild_user) {
                            //If the ServerID isn't findable
                            console.log(colors.green(`Can't find the ServerID of the user data`))
                            Delete_User_data_and_Streaming_Status(Streaming_User)
                            const User = bot.bot.users.find(u => u.id == Streaming_User.UserID)
                            if (User) {
                                User.createDM()
                                    .then(Util.SQL_DM_Invalid(Streaming_User.UserID, Streaming_User, "ServerID"))
                            }
                            return;
                        }

                        var channel_user = guild_user.channels.find(c => c.id == Streaming_User.ChannelID)
                        if (!channel_user) {
                            //If the ChannelID isn't findable
                            console.log(colors.green(`Can't find the ChannelID of the user data`))
                            Delete_User_data_and_Streaming_Status(Streaming_User)
                            const User = bot.bot.users.find(u => u.id == Streaming_User.UserID)
                            if (User) {
                                User.createDM()
                                    .then(Util.SQL_DM_Invalid(Streaming_User.UserID, Streaming_User, "ChannelID"))
                            }
                            return;
                        }

                        SelectAll_Queue(Streaming_User.UserTwitch, Streaming_User.ServerID, Streaming_User.ChannelID)
                            .then(result => {
                                if (!result) {
                                    //console.log(colors.green(`result`))
                                    console.log(result)
                                    //USED addUser(Streaming_User)
                                    Add_User_data_and_Streaming_Status(Streaming_User)
                                }
                            })

                        /*
                        if (!Streaming_User.MessageID) {
                            //Si le message de notif est PAS envoyé

                            console.log(colors.green(`MessageID=NULL`))
                            if (guild_user && channel_user) {
                                //We send the notification here
                                Util.SQL_Announce_Stream(Streaming_User, dataUser, false)
                            }

                        } else if (Streaming_User.MessageID != null) {
                            //Si le message de notif EST envoyé
                            console.log(colors.green(`MessageID=${Streaming_User.MessageID}`))
                            var message_user = channel_user.fetchMessage(Streaming_User.MessageID)
                                .then(async msg => {
                                    console.log(colors.green("Finded the msg"))
                                    //msg.edit("TEST DE OUF")
                                })
                                .catch(console.error);

                            if (guild_user && channel_user && message_user) {
                                //console.log(colors.green(message_user)

                            }

                        }
                        */

                    }
                })
                .catch(error => {
                    console.error(error)
                    console.error(Streaming_User.UserTwitch)
                })
        }
RisedSky commented 4 years ago

Hello i'm back after i don't know how long, i found that the "_id" undefined error is likely an error about the way that your code don't that this user doesn't exist. Checked the user and it seems right ! i will do a PR to fix this error of your code :)