实例化SpotifyWebApi对象时遭遇TypeError错误求助
解决spotify-web-api-node实例化报错问题
错误原因
你直接调用了SpotifyWebApi(),但这是一个类构造函数,必须使用new关键字来创建实例,否则this会指向全局对象(严格模式下为undefined),导致无法设置_credentials属性。
解决方案
修改实例化代码,添加new关键字:
const SpotifyWebApi = require('spotify-web-api-node'); // 不传credentials参数,后续可通过setClientId等方法补充配置 const spotifyApp = new SpotifyWebApi();
如果需要初始化时直接传入凭证信息,可按如下方式编写:
const SpotifyWebApi = require('spotify-web-api-node'); const spotifyApp = new SpotifyWebApi({ clientId: '你的客户端ID', clientSecret: '你的客户端密钥' });
补充说明
类构造函数签名constructor(credentials?: Credentials)仅表示参数可选,但无论是否传参,都必须遵循JavaScript类的基本规则——用new关键字实例化对象。
内容的提问来源于stack exchange,提问作者omershafrir
相关产品推荐
相关产品推荐

