I'm developing an application in Sails.js for a project at my university, which uses SoundCloud's API to retrieve a JSON object based on what the user searches for. The first function creates an entry in the DB with the title, and the second function retrieves data from SoundCloud based on the title, including duration, URL, and actual title (from the data retrieved).
When the user searches for a title, the input is passed through a , where the input id & name is "title". As far as I can tell, this part works just fine, but the error occurs when it gets to the "result" function. The error, in detail below, says TypeError: Request path contains unescaped characters
SoundCloudController.js:
module.exports = {
new: function (req, res) {
res.view();
},
search: function (req, res, next) {
SoundCloud.create(req.params.all(), function SCSoundCreated(err, SCSound) {
if (err) return next(err);
res.redirect('/soundcloud/result/' + SCSound.id);
});
},
// Search for something
// SoundCloud API reference:
// https://developers.soundcloud.com/docs/api/reference#tracks
result: function (req, res, next) {
SoundCloud.findOne(req.param('id')).populateAll().exec(function (err, SCSound) {
if (err) return next(err);
if (!SCSound) return next();
var http = require('http');
// Our client_id, needed to use the API
var client_id = 'client_id_here';
function process_response(webservice_response, SCSound, 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)
SCSound_data = JSON.parse(webservice_data);
// Find the title of the first match
SCSound.songtitle = SCSound_data.title;
// The duration provided by SoundCloud is in milliseconds
// convert to MM:SS format for readability
SCSound.duration = millis_to_min_sec(SCSound_data.duration);
// URL for track
SCSound.url = SCSound_data.permalink_url;
console.log(SCSound.title + ' ' + SCSound.duration);
callback();
});
};
// Define host, path etc. for the search (JSON returned)
function get_sound_data(SCSound, callback) {
//http://api.soundcloud.com/tracks.json?client_id=CLIENT_ID_HERE&q=smile%20like%20you%20mean%20it
console.log(SCSound.title);
options = {
host: 'http://api.soundcloud.com',
port: 80,
path: '/tracks.json?client_id=' + client_id + '&q=' + SCSound.title + '&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, SCSound, callback)
});
webservice_request.end();
console.log(SCSound.title + ' ' + SCSound.duration)
};
async.each([SCSound], get_sound_data, function (err) {
if (err) console.log(err);
console.log('done');
res.view({
SCSound: SCSound
});
});
// 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;
};
});
},
};
The view where the result is displayed; result.ejs:
<div id="resultList" class="main-container result-shadow">
<!-- Search result for SoundCloud -->
<ul class="collection black-text">
<!-- SoundCloud result -->
<!-- SoundCloud Search API: https://developers.soundcloud.com/docs/api/guide#search -->
<li class="collection-item avatar">
<img id="providerLogo"
src=""
alt=""
class="circle">
<!-- Song Title -->
<span class="title"></span>
<!-- Artist | Not applicable for SoundCloud results, as they have no unique field for artist -->
<p>
<!--
The Killers
<br> -->
<!-- Length -->
</p>
<!-- Link to track -->
<a href="" target="_blank"
class="secondary-content"><i class="material-icons">arrow_forward</i></a>
</li>
<!-- End of SoundCloud result -->
</ul>
</div>
And finally; the error:
C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\protocol\Parser.js:77
throw err; // Rethrow non-MySQL errors
^
TypeError: Request path contains unescaped characters
at new ClientRequest (_http_client.js:53:11)
at Object.exports.request (http.js:31:10)
at get_sound_data (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\api\controllers\SoundCloudController.js:72:47)
at C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails\node_modules\async\lib\async.js:181:20
at Object.async.forEachOf.async.eachOf (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails\node_modules\async\lib\async.js:233:13)
at Object.async.forEach.async.each (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails\node_modules\async\lib\async.js:209:22)
at C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\api\controllers\SoundCloudController.js:80:19
at wrapper (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\lodash\index.js:3592:19)
at applyInOriginalCtx (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\waterline\lib\waterline\utils\normalize.js:421:80)
at wrappedCallback (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\waterline\lib\waterline\utils\normalize.js:324:18)
at success (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\waterline\node_modules\switchback\lib\normalize.js:33:31)
at _switch (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\waterline\node_modules\switchback\lib\factory.js:58:28)
at returnResults (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\waterline\lib\waterline\query\finders\basic.js:179:9)
at C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\waterline\lib\waterline\query\finders\basic.js:86:16
at C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\waterline\lib\waterline\query\finders\operations.js:83:7
at C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\async\lib\async.js:52:16
at Object.async.forEachOf.async.eachOf (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\async\lib\async.js:236:30)
at Object.async.forEach.async.each (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\async\lib\async.js:209:22)
at C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\waterline\lib\waterline\query\finders\operations.js:436:11
at C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\waterline\lib\waterline\query\finders\operations.js:574:5
at C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\async\lib\async.js:52:16
at Object.async.forEachOf.async.eachOf (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\async\lib\async.js:236:30)
Process finished with exit code 1
Any help will be greatly appreciated.
via Morten Amundsen
No comments:
Post a Comment