wzhouwzhou / ytsearcher

YTSearcher | 170k+ DL | 1000+ Dependents | NodeJS package providing an easy-to-use promise-based solution for getting youtube search results.
https://ytsearcher.willyz.cf
Apache License 2.0
18 stars 2 forks source link

YTSearcher is not a constructor issue #1

Closed icq4ever closed 6 years ago

icq4ever commented 6 years ago

writing codes on index.js in this repository, works. but with npm install, cannot initilize new ytsearcher object with new keyword.

error log

/home/icq4ever/nodejs/test/ytpage/app.js:2
var searcher = new YTSearcher({
               ^

TypeError: YTSearcher is not a constructor
    at Object.<anonymous> (/home/icq4ever/nodejs/test/ytpage/app.js:2:16)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3

simple app.js


var YTSearcher = require('ytsearcher');
var searcher = new YTSearcher({
    key : 'YOUTUBE_API_KEY',
    revealkey : true,
});

var query = "bbc";
const options= {
    maxResults : '50',
    part : 'snippet',
    type : 'video'
}

searcher.search(query, options).then(seaarchResult=>{
    const currentPage = searchResult.currentPage;
    console.log(currentPage);
})
wzhouwzhou commented 6 years ago

Good afternoon @icq4ever and thank you for your interest in improving this package.

I understand you are having trouble instantiating a YTSearcher object. While most likely unrelated to the current issue, the latest release version of 1.1.3 contains various bug fixes and improvements from previous versions, so please make sure you are up-to-date.

That said, I've determined that you are incorrectly importing the YTSearcher object itself. If you check the docs/Readme or requiring the package in a repl, you'll note that ytsearcher exports several items. You must use a destructuring assignment when importing the YTSearcher object.

In short, you cannot assign the exports directly to YTSearcher and call new on it. As quoted from README.md, you must use:

Creating the object: const { YTSearcher } = require('ytsearcher');

and then create a new object with new YTSearcher({ etc. You can also assign YTSearcher with YTSearcher = require('ytsearcher').YTSearcher;

Thank you for your time, and if you have any other questions feel free to let me know. Have a good day.

~ William

Issue #1 Marked as Closed Wed July 31 Do you enjoy using this package? If so, consider leaving a star on this repository, and checking out my other work, including my latest release easypathutil! Have an improvement you want to contribute? If so, please consider creating a pull request! Thank you for your time opening this issue, I hope you found my support useful, and if you have any other questions feel free to let me know.