You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

实例化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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 07:40:42