Sails.js - 当 运行 函数来自 Controller 时出现 TypeError
Sails.js - TypeError when running function from Controller
我正在 Sails.js 为我大学的一个项目开发一个应用程序,它使用 SoundCloud 的 API 根据用户的内容检索 JSON object搜索。第一个函数使用标题在数据库中创建一个条目,第二个函数根据标题从 SoundCloud 检索数据,包括持续时间、URL 和实际标题(来自检索到的数据)。
当用户搜索标题时,输入通过 a 传递,其中输入 id & name 为 "title"。据我所知,这部分工作正常,但在到达 "result" 函数时发生错误。
该错误在下面详细说明 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;
};
});
},
};
显示结果的视图; 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>
最后;错误:
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
任何帮助将不胜感激。
正如 @Sangharsh 在评论中指出的那样,我需要通过将路径从 path: '/tracks.json?client_id=' + client_id + '&q=' + SCSound.title + '&limit=2'
更改为 path: '/tracks.json?client_id=' + client_id + '&q=' + encodeURIComponent(SCSound.title) + '&limit=2'
来对 URL 参数进行编码。
我正在 Sails.js 为我大学的一个项目开发一个应用程序,它使用 SoundCloud 的 API 根据用户的内容检索 JSON object搜索。第一个函数使用标题在数据库中创建一个条目,第二个函数根据标题从 SoundCloud 检索数据,包括持续时间、URL 和实际标题(来自检索到的数据)。
当用户搜索标题时,输入通过 a 传递,其中输入 id & name 为 "title"。据我所知,这部分工作正常,但在到达 "result" 函数时发生错误。
该错误在下面详细说明 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;
};
});
},
};
显示结果的视图; 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>
最后;错误:
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
任何帮助将不胜感激。
正如 @Sangharsh 在评论中指出的那样,我需要通过将路径从 path: '/tracks.json?client_id=' + client_id + '&q=' + SCSound.title + '&limit=2'
更改为 path: '/tracks.json?client_id=' + client_id + '&q=' + encodeURIComponent(SCSound.title) + '&limit=2'
来对 URL 参数进行编码。