我正在尝试将spotify api与节点js和角度js连接。每当我尝试获取特定艺术家的详细信息时,我都会收到此错误。
app.js
var app = angular.module('angularjs-starter', ['jsonService', 'ngRoute', 'ngResource'])
app.controller('MainCtrl', function($scope, JsonService) {
//JsonService.get(function(data) {
// $scope.name = data.artists.href;
// $scope.children = data.artists.items;
//});
$scope.searchShow = () => {
JsonService.search.query({
show: $scope.showname
}, (response) => {
console.log(response);
// $scope.name = response.artists.href;
$scope.childr = response;
$scope.children = response;
})
}
$scope.showDetails = (id) => {
console.log(id);
JsonService.detail.query({
details: id
}, (response) => {
console.log(response);
// $scope.name = response.artists.href;
})
}
});
app.config(['$locationProvider', function($locationProvider) {
$locationProvider.hashPrefix('')
}])
app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/search.html',
controller: 'MainCtrl'
})
.when('/details', {
templateUrl: 'views/details.html',
controller: 'MainCtrl'
})
.otherwise({
redirectTo: '/'
})
}])
service.js
angular.module('jsonService', ['ngResource'])
.factory('JsonService', function($resource) {
return {
search: $resource('/api/search'),
detail: $resource('/api/details')
}
});
routes.js-来自节点js服务器
const
express = require('express'),
path = require('path'),
router = express.Router(),
superagent = require('superagent')
module.exports = () => {
router.get('/api/search', (req, res) => {
const { show } = req.query // this is the same as const show = req.query.show
console.log(show);
superagent
.get('https://api.spotify.com/v1/search?q=' + show + ':&type=artist')
.end((err, response) => {
if (err) {
res.send(err);
console.log(err);
} else {
console.log(response.body.artists.items);
res.json(response.body.artists.items);
}
})
})
router.get('/api/details', (req, res) => {
const { details } = req.query // this is the same as const show = req.query.show
console.log(details);
superagent
.get('https://api.spotify.com/v1/artists/' + details)
.end((err, response) => {
if (err) {
res.send(err);
console.log(err);
} else {
res.json(response.body);
}
})
})
router.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../client/index.html'))
})
return router
}
节点js能够从细节中获取值,但我无法获取ui的值
答案 0 :(得分:1)
当API返回单个对象时,请使用get
操作方法:
$scope.showDetails = (id) => {
console.log(id);
//JsonService.detail.query({
//USE get action method
JsonService.detail.get({
details: id
}, (response) => {
console.log(response);
// $scope.name = response.artists.href;
})
}
当API返回数组时,请使用query
操作方法。
错误:
$resource:badcfg
响应与配置的参数
不匹配描述
当$resource服务期望响应可以作为数组反序列化但接收对象时,会发生此错误,反之亦然。默认情况下,所有资源操作都需要对象,但需要数组的
query
除外。要解决此错误,请确保您的$resource配置与从服务器返回的数据的实际格式相匹配。
有关详细信息,请参阅$resource API reference documentation。