talmobi / yt-search

96 stars 30 forks source link

Cannot read properties of undefined (reading 'slice') #70

Closed dheepankarthik closed 2 years ago

dheepankarthik commented 2 years ago

I'm getting this error.

$ node .
undefined
D:\Discord\Discord Bots\Testing\Testing yt-search\index.js:5
const videos = r.videos.slice( 0, 3 )
                        ^

TypeError: Cannot read properties of undefined (reading 'slice')
    at Object.<anonymous> (D:\Discord\Discord Bots\Testing\Testing yt-search\index.js:5:25)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

Node.js v17.1.0

I tried by using TypeScript:

const yts = require( 'yt-search' )
const r: Object | String[] = yts( 'superman theme' )

yts()

const videos = r.videos.slice( 0, 3 )
videos.forEach( function ( v: Object) {
    const views = String( v.views ).padStart( 10, ' ' )
    console.log( `${ views } | ${ v.title } (${ v.timestamp }) | ${ v.author.name }` )
} )

But it shows the following errors:

$ tsc index.ts
index.ts:6:18 - error TS2339: Property 'videos' does not exist on type 'Object | String[]'.
  Property 'videos' does not exist on type 'Object'.

6 const videos = r.videos.slice( 0, 3 )
                   ~~~~~~

index.ts:8:26 - error TS2339: Property 'views' does not exist on type 'Object'.

8  const views = String( v.views ).padStart( 10, ' ' )
                           ~~~~~

index.ts:9:34 - error TS2339: Property 'title' does not exist on type 'Object'.

9  console.log( `${ views } | ${ v.title } (${ v.timestamp }) | ${ v.author.name }` )
                                   ~~~~~

index.ts:9:48 - error TS2339: Property 'timestamp' does not exist on type 'Object'.

9  console.log( `${ views } | ${ v.title } (${ v.timestamp }) | ${ v.author.name }` )
                                                 ~~~~~~~~~

index.ts:9:68 - error TS2339: Property 'author' does not exist on type 'Object'.

9  console.log( `${ views } | ${ v.title } (${ v.timestamp }) | ${ v.author.name }` )
                                                                     ~~~~~~

Found 5 errors.

I am stuck.

talmobi commented 2 years ago

You have to either use the Promises (with await for example) or give a callback function -- you are doing neither.

See the README example:

const yts = require( 'yt-search' )
const r = await yts( 'superman theme' )

const videos = r.videos.slice( 0, 3 )
videos.forEach( function ( v ) {
    const views = String( v.views ).padStart( 10, ' ' )
    console.log( `${ views } | ${ v.title } (${ v.timestamp }) | ${ v.author.name }` )
} )
dheepankarthik commented 2 years ago

@talmobi I really appreciate the immediate action for this issue. But, I kinda actually found out a own solution:

const yts = require( 'yt-search' )

let sigmaRule = async () => {
    let search = await yts("Sigma Rule")
    //console.log(search['videos'])

    const videos = search['videos'].slice( 0, 3 )
    videos.forEach( function ( v ) {
        const views = String( v.views ).padStart( 10, ' ' )
        console.log( `${ views } | ${ v.title } (${ v.timestamp }) | ${ v.author.name } | ${v.videoId}` )
    } )

}

sigmaRule()

The output:

$ node .
   3391292 | Sigma Rules | Sigma Rule Videos Compilation🔥 (5:30) | Prakhar Dwivedi | KR-DieLS9Y8
   3158468 | Sigma Attitude Rules | Sigma Memes | Sigma Male Attitude Videos | Attitude Boy Sigma Memes 🔥 (15:11) | Dishoom Boy | 2f9yv-T1QNM
    115078 | Sigma Rules Memes Part 6 | Sigma Memes Compilation | Sigma Male Grindest 🔥 (11:17) | Dishoom Boy | eVT7ru51jl8

I hope this is a right and good way to do it. Actually, I think you need to use it as ['videos'] in const videos = search['videos'].slice( 0, 3 ), cuz the actual code inputs it as {all: [...], videos: [...]}. Nothing that you don't know mate 👍🏻

Developer Sigma Rule 1: Name your function as sigmaRule()

dheepankarthik commented 2 years ago

BTW remove the invalid tag. My code may possibly be a correction. Not tryna be bossy, just informing 😄

talmobi commented 2 years ago

@dheepankarthik glad you got it working~ That indeed looks correct.

Previously you did not call the library correctly (either using a callback function or handling the promise with ex: await) -- this is why I put the invalid? tag because it seems like it wasn't a bug with the library itself.

To explain what you did wrong:

// you are not giving a `callback` function so this will return a Promise.
//. you have to resolve the Promise somehow (for example using `await` keyword) to get the result,
// but you are not doing it here which is why "videos" is undefined.
const r: Object | String[] = yts( 'superman theme' )

// this does nothing. you are calling the library without any arguments and without grabbing or handling
// the Promise nor supplied a callback value
yts()

If everything is OK and you have no more questions please feel free to close this issue~

dheepankarthik commented 2 years ago

Thank you for the explanation. I will close this issue.