我正在使用Spotify Web Api Node拨打电话。
我的问题是我在getMe api调用返回数据之前被重定向...
如何等待结果然后重定向?
抱歉,我是NodeJS和Express的新手。
var express = require('express');
var router = express.Router();
/* GET callback. */
router.get('/', function(req, res, next) {
console.log('callback worked!')
var code = req.query.code || null;
// var state = req.query.state || null;
// var storedState = req.cookies ? req.cookies[stateKey] : null;
req.app.settings.spotifyApi.authorizationCodeGrant(code)
.then(function(data) {
console.log('The token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
console.log('The refresh token is ' + data.body['refresh_token']);
// Set the access token on the API object to use it in later calls
req.app.settings.spotifyApi.setAccessToken(data.body['access_token']);
req.app.settings.spotifyApi.setRefreshToken(data.body['refresh_token']);
req.app.settings.spotifyApi.getMe().then(function(data) {
if (typeof req.session.user === 'undefined') {
req.session.user = {};
}
// never gets here
console.log(req.session.user);
req.session.user.display_name = data.body.display_name
req.session.user.email = data.body.email
req.session.user.id = data.body.id
}, function(err) {
console.log('Something went wrong!', err);
});
}, function(err) {
console.log('Something went wrong!', err);
});
res.redirect(302, '/');
});
module.exports = router;
答案 0 :(得分:2)
如果您在错误或成功后需要重定向作为最后一步,请尝试此解决方案:
var express = require('express');
var router = express.Router();
/* GET callback. */
router.get('/', function(req, res, next) {
console.log('callback worked!')
var code = req.query.code || null;
// var state = req.query.state || null;
// var storedState = req.cookies ? req.cookies[stateKey] : null;
req.app.settings.spotifyApi.authorizationCodeGrant(code)
.then(function(data) {
console.log('The token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
console.log('The refresh token is ' + data.body['refresh_token']);
// Set the access token on the API object to use it in later calls
req.app.settings.spotifyApi.setAccessToken(data.body['access_token']);
req.app.settings.spotifyApi.setRefreshToken(data.body['refresh_token']);
return req.app.settings.spotifyApi.getMe().then(function(data) {
if (typeof req.session.user === 'undefined') {
req.session.user = {};
}
// never gets here
console.log(req.session.user);
req.session.user.display_name = data.body.display_name
req.session.user.email = data.body.email
req.session.user.id = data.body.id
}, function(err) {
console.log('Something went wrong!', err);
});
}, function(err) {
console.log('Something went wrong!', err);
})
.then(() => {
res.redirect(302, '/');
})
});
module.exports = router;
答案 1 :(得分:0)
谢谢,
这是我的工作代码:
var express = require('express');
var router = express.Router();
/* GET callback. */
router.get('/', function(req, res, next) {
console.log('callback worked!')
var code = req.query.code || null;
// var state = req.query.state || null;
// var storedState = req.cookies ? req.cookies[stateKey] : null;
req.app.settings.spotifyApi.authorizationCodeGrant(code)
.then(function(data) {
console.log('The token expires in ' + data.body['expires_in']);
console.log('The access token is ' + data.body['access_token']);
console.log('The refresh token is ' + data.body['refresh_token']);
// Set the access token on the API object to use it in later calls
req.app.settings.spotifyApi.setAccessToken(data.body['access_token']);
req.app.settings.spotifyApi.setRefreshToken(data.body['refresh_token']);
}, function(err) {
console.log('Something went wrong!', err);
})
.then(function(data) {
req.app.settings.spotifyApi.getMe().then(function(data) {
req.session.user.display_name = data.body.display_name
req.session.user.email = data.body.email
req.session.user.id = data.body.id
console.log(req.session.user);
res.redirect(302, '/');
}, function(err) {
console.log('Something went wrong!', err);
res.redirect(302, '/');
});
});
});
module.exports = router;