Closed dheepankarthik closed 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 }` )
} )
@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()
BTW remove the invalid tag. My code may possibly be a correction. Not tryna be bossy, just informing 😄
@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~
Thank you for the explanation. I will close this issue.
I'm getting this error.
I tried by using TypeScript:
But it shows the following errors:
I am stuck.