Return 来自模块未定义
Return from module is undefined
嗨,我制作了一个模块,当我尝试 return 歌曲名称时,它只是未定义,console.log 就在它工作之前,所以我有点困惑。 console.log(`"${songName}" by ${artistsList}`) 之后的那一行是问题所在,我尝试 return "${songName}" by ${artistsList}
但 return 是只是未定义所以我有点不明白为什么
代码:(这是spotify.js模块)
我用 const Spotify = require("./spotify.js") 调用它,一切正常,直到我只需要 return 使用 Spotify.request() 函数
的歌曲名称]
const axios = require("axios");
module.exports = {
token: "",
ref: "",
refresh: function () {
axios({
method: "post", //you can set what request you want to be
url: "https://accounts.spotify.com/api/token",
data:
`grant_type=refresh_token&refresh_token=${this.ref}`,
headers: {
Authorization:
"Basic ",
"Content-Type": "application/x-www-form-urlencoded"
}
}).then(response => {
this.token = response.data.access_token
console.log(`Successfully refreshed token ${module.exports.token}`);
});
},
analyseSong: function (data) {
console.log(data)
const x = data;
//console.log(x)
if (x.is_playing) {
const songNameMatch = x.item.name.split("(");
var songName = x.item.name;
try {
const letter = songNameMatch[1].charAt(0);
letter.toLowerCase() == "f" ||
songNameMatch[1].toLowerCase().match(/^with/)
? (songName = songNameMatch[0].trim())
: false;
} catch (err) {
false;
}
var artistArray = [];
var artist;
const artists = x.item.artists;
//console.log(token)
for (artist in artists) {
artistArray.push(x.item.artists[artist].name);
}
//console.log(artistArray)
const artistsList = artistArray.join(", ");
//console.log(artistsList)
//console.log(`Currently playing: ${songName} - ${artistsList}`);
console.log(`"${songName}" by ${artistsList}`)
return `"${songName}" by ${artistsList}`;
} else {
return `Not currently listening to a song`;
}
},
request: function () {
axios
.get("https://api.spotify.com/v1/me/player/currently-playing", {
headers: { Authorization: `Bearer ${this.token}` }
})
.then(function (response) {
module.exports.analyseSong(response.data);
})
},
}
So I'm doing X=Spotify.request()
trying to get the song name as X so I can output using IRC chat
有几个问题。 .request()
根本没有 return 值。因此,它永远不会 return 除了 undefined
以外的任何东西。您可以通过添加两个 return
语句来解决这个问题,第一个 return 承诺本身,第二个使 analyseSong()
中的 return 值成为解析值承诺:
request: function () {
return axios
.get("https://api.spotify.com/v1/me/player/currently-playing", {
headers: { Authorization: `Bearer ${this.token}` }
})
.then(function (response) {
return module.exports.analyseSong(response.data);
})
},
现在 Spotify.request()
将 return 一个承诺,该承诺将使用 return 来自 .analyseSong(response.data)
的值解析。您可以像这样使用该承诺:
Spotify.request().then(result => {
console.log(result);
}).catch(err => {
console.log(err);
});
嗨,我制作了一个模块,当我尝试 return 歌曲名称时,它只是未定义,console.log 就在它工作之前,所以我有点困惑。 console.log(`"${songName}" by ${artistsList}`) 之后的那一行是问题所在,我尝试 return "${songName}" by ${artistsList}
但 return 是只是未定义所以我有点不明白为什么
代码:(这是spotify.js模块) 我用 const Spotify = require("./spotify.js") 调用它,一切正常,直到我只需要 return 使用 Spotify.request() 函数
的歌曲名称]const axios = require("axios");
module.exports = {
token: "",
ref: "",
refresh: function () {
axios({
method: "post", //you can set what request you want to be
url: "https://accounts.spotify.com/api/token",
data:
`grant_type=refresh_token&refresh_token=${this.ref}`,
headers: {
Authorization:
"Basic ",
"Content-Type": "application/x-www-form-urlencoded"
}
}).then(response => {
this.token = response.data.access_token
console.log(`Successfully refreshed token ${module.exports.token}`);
});
},
analyseSong: function (data) {
console.log(data)
const x = data;
//console.log(x)
if (x.is_playing) {
const songNameMatch = x.item.name.split("(");
var songName = x.item.name;
try {
const letter = songNameMatch[1].charAt(0);
letter.toLowerCase() == "f" ||
songNameMatch[1].toLowerCase().match(/^with/)
? (songName = songNameMatch[0].trim())
: false;
} catch (err) {
false;
}
var artistArray = [];
var artist;
const artists = x.item.artists;
//console.log(token)
for (artist in artists) {
artistArray.push(x.item.artists[artist].name);
}
//console.log(artistArray)
const artistsList = artistArray.join(", ");
//console.log(artistsList)
//console.log(`Currently playing: ${songName} - ${artistsList}`);
console.log(`"${songName}" by ${artistsList}`)
return `"${songName}" by ${artistsList}`;
} else {
return `Not currently listening to a song`;
}
},
request: function () {
axios
.get("https://api.spotify.com/v1/me/player/currently-playing", {
headers: { Authorization: `Bearer ${this.token}` }
})
.then(function (response) {
module.exports.analyseSong(response.data);
})
},
}
So I'm doing
X=Spotify.request()
trying to get the song name as X so I can output using IRC chat
有几个问题。 .request()
根本没有 return 值。因此,它永远不会 return 除了 undefined
以外的任何东西。您可以通过添加两个 return
语句来解决这个问题,第一个 return 承诺本身,第二个使 analyseSong()
中的 return 值成为解析值承诺:
request: function () {
return axios
.get("https://api.spotify.com/v1/me/player/currently-playing", {
headers: { Authorization: `Bearer ${this.token}` }
})
.then(function (response) {
return module.exports.analyseSong(response.data);
})
},
现在 Spotify.request()
将 return 一个承诺,该承诺将使用 return 来自 .analyseSong(response.data)
的值解析。您可以像这样使用该承诺:
Spotify.request().then(result => {
console.log(result);
}).catch(err => {
console.log(err);
});