我正在开发一个控制器,使用以下代码(SoundCloudController.js)从SoundCloud的API解析JSON,以便检索一些数据 . 当我抬起应用程序,然后转到我创建的视图; “result.ejs”,它崩溃了 .
module.exports = {
// Search for something
// SoundCloud API reference:
// https://developers.soundcloud.com/docs/api/reference#tracks
result: function (req, res, next) {
SoundCloud.find(function foundSounds(err, sounds) {
if (err) return next(err);
var http = require('http');
function process_response(webservice_response, sound, callback) {
var webservice_data = "";
webservice_response.on('error', function (e) {
console.log(e.message);
callback("Error: " + e.message);
});
webservice_response.on('data', function (chunk) {
webservice_data += chunk;
});
// Response from query
webservice_response.on('end', function () {
// Parse everything from the response (JSON)
sound_data = JSON.parse(webservice_data);
// Find the title of the first match
sound.title = sound_data.title;
// The duration provided by SoundCloud is in milliseconds
// convert to MM:SS format for readability
sound.duration = millis_to_min_sec(sound_data.duration);
// Logo for SoundCloud
sound.logo = 'http://icons.iconarchive.com/icons/sicons/basic-round-social/512/soundcloud-icon.png';
console.log(sound.title + ' ' + sound.duration);
callback();
});
};
// Define host, path etc. for the search (JSON returned)
function get_sound_data(sound, callback) {
//http://api.soundcloud.com/tracks.json?client_id=CLIENT_ID_HERE&q=smile%20like%20you%20mean%20it
console.log(sound.title);
console.log(sound.duration);
options = {
host: 'http://api.soundcloud.com',
port: 80,
path: '/tracks.json?client_id=O3UkayfZTJjNeahVhqTiHcZ5iowrMRpk&q=smile like you mean it&limit=2', // client_id is given above, q='something to search for', limit to 2 results
method: 'GET'
};
var webservice_request = http.request(options, function (response) {
process_response(response, sound, callback)
});
webservice_request.end();
};
// Convert milliseconds to MM:SS format (minutes:seconds)
function millis_to_min_sec(millis) {
var minutes = Math.floor(millis / 60000);
var seconds = ((millis % 60000) / 1000).toFixed(0);
return minutes + ':' + (seconds < 10 ? '0' : '') + seconds;
};
async.each(sound.sounds, get_sound_data, function(err) {
if(err) console.log(err);
console.log('done');
res.view({
sound: sound
});
});
});
},
};
访问localhost时出现的错误消息:1337 / music / result:
错误:发送500(“服务器错误”)响应:错误(E_UNKNOWN)::遇到意外错误:ER_BAD_FIELD_ERROR:Query.Sequence._packetToError(C:\ Users \ InWhi \)中'where子句'中的未知列'NaN'在Query.ErrorPacket上的Desktop \ ISQA4380 \ Group-6-Project \ node_modules \ sails-mysql \ node_modules \ mysql \ lib \ protocol \ sequences \ Sequence.js:48:14)(C:\ Users \ InWhi \ Desktop \ ISQA4380 \在Protocol._parsePacket(C:\ Users \ InWhi \ Desktop \ ISQA4380 \ Group-6-)中的Group-6-Project \ node_modules \ sails-mysql \ node_modules \ mysql \ lib \ protocol \ sequences \ Query.js:83:18) Parser.write上的Project \ node_modules \ sails-mysql \ node_modules \ mysql \ lib \ protocol \ Protocol.js:280:23)(C:\ Users \ InWhi \ Desktop \ ISQA4380 \ Group-6-Project \ node_modules \ sails-在Protocol.write的mysql \ node_modules \ mysql \ lib \ protocol \ Parser.js:73:12)(C:\ Users \ InWhi \ Desktop \ ISQA4380 \ Group-6-Project \ node_modules \ sails-mysql \ node_modules \ mysql \ lib \ protocol \ Protocol.js:39:16)在Socket . (C:\ Users \ InWhi \ Desktop \ ISQA4380 \ Group-6-Project \ node_modules \ sails-mysql \ node_modules \ mysql \ lib \ Connection.js:96:28)在emitOne(events.js:96:13)at at在socket.adread(net.js:548:20)的Socket.Readable.push(_stream_readable.js:134:10)处的readableAddChunk(_stream_readable.js:176:18)处的Socket.emit(events.js:188:7) )--------------------在Protocol._enqueue(C:\ Users \ InWhi \ Desktop \ ISQA4380 \ Group-6-Project \ node_modules \ sails-mysql \ node_modules) PoolConnection.query上的\ mysql \ lib \ protocol \ Protocol.js:141:48)(C:\ Users \ InWhi \ Desktop \ ISQA4380 \ Group-6-Project \ node_modules \ sails-mysql \ node_modules \ mysql \ lib \ Connection .js:201:25)之后在FIND(C:\ Users \ InWhi \ Desktop \ ISQA4380 \ Group-6-Project \ node_modules \ sails-mysql \ lib \ adapter.js:838:20)之后(C:\ Users) \ InWhi \ Desktop \ ISQA4380 \ Group-6-Project \ node_modules \ sails-mysql \ lib \ connections \ spawn.js:84:5)在C:\ Users \ InWhi \ Desktop \ ISQA4380 \ Group-6-Project \ node_modules \ sails-mysql \ lib \ connections \ spawn.js:40:7在Ping.onOperationComplete [as _callback](C:\用户\ InWhi \ Desktop \ ISQA4380 \ Group-6-Project \ node_modules \ sails-mysql \ node_modules \ mysql \ lib \ Pool.js:99:5)在Ping.Sequence.end(C:\ Users \ InWhi \ Desktop \ ISQA4380 \ Group-6-Project \ node_modules \ sails-mysql \ node_modules \ mysql \ lib \ protocol \ sequences \ Sequence.js:96:24)在Ping.Sequence.OkPacket(C:\ Users \ InWhi \ Desktop \ ISQA4380 \在Protocol._parsePacket(C:\ Users \ InWhi \ Desktop \ ISQA4380 \ Group-6-)中的Group-6-Project \ node_modules \ sails-mysql \ node_modules \ mysql \ lib \ protocol \ sequences \ Sequence.js:105:8) Parser.write上的Project \ node_modules \ sails-mysql \ node_modules \ mysql \ lib \ protocol \ Protocol.js:280:23)(C:\ Users \ InWhi \ Desktop \ ISQA4380 \ Group-6-Project \ node_modules \ sails-在Protocol.write的mysql \ node_modules \ mysql \ lib \ protocol \ Parser.js:73:12)(C:\ Users \ InWhi \ Desktop \ ISQA4380 \ Group-6-Project \ node_modules \ sails-mysql \ node_modules \ mysql \ lib \ protocol \ Protocol.js:39:16)在Socket . (C:\ Users \ InWhi \ Desktop \ ISQA4380 \ Group-6-Project \ node_modules \ sails-mysql \ node_modules \ mysql \ lib \ Connection.js:96:28)在emitOne(events.js:96:13)at at在socket.adread(net.js:548:20)的Socket.Readable.push(_stream_readable.js:134:10)处的readableAddChunk(_stream_readable.js:176:18)处的Socket.emit(events.js:188:7) )详细信息:错误:ER_BAD_FIELD_ERROR:'where子句'中的未知列'NaN'
任何改进的建议和/或解决我的问题的解决方案,非常感谢!
1 回答
当你去
localhost:1337/music/result
时,似乎你无意中为一个名为music
的模型打了蓝图路线 . 它将result
作为/:model/:id
的URL格式的ID . 确保您的路由配置设置为覆盖该格式并指向SoundCloudController.result
.