如何在项目中移动axios实例



我在express.js/node.js项目中使用Axios,并试图传递Axios实例。

让我困惑的是,事情是如何在javascript中传递的,而许多事情并不是按代码顺序发生的。有人能告诉我实现这一目标的正确方法吗?目前,我正在从Spotify的API请求一个令牌,然后使用该令牌创建一个Axios实例,以便进一步调用Spotify。

spotify-auth.js

const axios = require('axios').default;
let config = require('../../config');
var spotifyAxios = undefined;
console.log('this isnt logging');
axios.request({
url: "/api/token",
method: "post",
baseURL: "https://accounts.spotify.com/",
auth: {
username: config.spotify_client_id,
password: config.spotify_client_secret,
},
params: {
"grant_type": "client_credentials",
},
headers: {
"Accept": 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
}
}).then(res => {
console.log(res.data);
try {
var token = res.data.access_token;
spotifyAxios = axios.create({
baseURL: 'https://api.spotify.com/v1/',
timeout: 1200,
headers: {'Authorization': 'Bearer'+token}
});
console.log("spotifyAxios created");
}
catch(err) {
console.log(err);
spotifyAxios = undefined;
}

}, (err) => {
console.log(err);
});
console.log('this isnt logging')
module.exports = spotifyAxios;

spotify.controller.js

const axios = require('axios').default;
const spotifyAxios = require('../services/spotify-auth');
exports.searchAlbum = function(req, res) {
console.log(req.params);
if (req.params.album == undefined) {
res.status(400).send("searching string undefined");
return;
}
try {
spotifyAxios.request({
method: 'get',
url: 'https://api.spotify.com/v1/search',
data: {
q: req.params.album,
type: "album",
}
}).then(res => {
console.log(res);
}, (err) => {
console.log(err);
});
}
catch(err) {
console.log(err); // making the API call leads to error here because spotifyAxios isn't defined (undefined)
res.status(500).send("unable to search albums");
}    
}

我想试试这个,因为API调用是Asycronus

spotify-auth.js

const axios = require('axios').default;
let config = require('../../config');
//var spotifyAxios = undefined;
console.log('this isnt logging');
async function spotifyAxios() {
await axios.request({
url: "/api/token",
method: "post",
baseURL: "https://accounts.spotify.com/",
auth: {
username: config.spotify_client_id,
password: config.spotify_client_secret,
},
params: {
"grant_type": "client_credentials",
},
headers: {
"Accept": 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
}
}).then(async res => {
console.log(res.data);
try {
var token = res.data.access_token;
return await axios.create({
baseURL: 'https://api.spotify.com/v1/',
timeout: 1200,
headers: {'Authorization': 'Bearer'+token}
});
console.log("spotifyAxios created");
}
catch(err) {
console.log(err);
spotifyAxios = undefined;
}

}, (err) => {
console.log(err);
});
}
console.log('this isnt logging')
module.exports = spotifyAxios;

spotify.controller.js

const axios = require('axios').default;
const spotifyAxios = require('../services/spotify-auth');
exports.searchAlbum = async function(req, res) {
console.log(req.params);
if (req.params.album == undefined) {
res.status(400).send("searching string undefined");
return;
}
try {
await spotifyAxios.request({
method: 'get',
url: 'https://api.spotify.com/v1/search',
data: {
q: req.params.album,
type: "album",
}
}).then(res => {
console.log(res);
}, (err) => {
console.log(err);
});
}
catch(err) {
console.log(err); // making the API call leads to error here because spotifyAxios isn't defined (undefined)
res.status(500).send("unable to search albums");
}    
}

最新更新