0
votes

I'm developing a controller to parse JSON from SoundCloud's API, using the following code (SoundCloudController.js), in order to retrieve some data. When I lift the application, and go to a view I created; "result.ejs", it crashes.

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
                });
            });
        });
    },

};

The Error message that appears when visiting localhost:1337/music/result:

error: Sending 500 ("Server Error") response: Error (E_UNKNOWN) :: Encountered an unexpected error : ER_BAD_FIELD_ERROR: Unknown column 'NaN' in 'where clause' at Query.Sequence._packetToError (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\protocol\sequences\Sequence.js:48:14) at Query.ErrorPacket (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\protocol\sequences\Query.js:83:18) at Protocol._parsePacket (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\protocol\Protocol.js:280:23) at Parser.write (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\protocol\Parser.js:73:12) at Protocol.write (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\protocol\Protocol.js:39:16) at Socket. (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\Connection.js:96:28) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at readableAddChunk (_stream_readable.js:176:18) at Socket.Readable.push (_stream_readable.js:134:10) at TCP.onread (net.js:548:20) -------------------- at Protocol._enqueue (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\protocol\Protocol.js:141:48) at PoolConnection.query (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\Connection.js:201:25) at FIND (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\lib\adapter.js:838:20) at afterwards (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\lib\connections\spawn.js:84:5) at C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\lib\connections\spawn.js:40:7 at Ping.onOperationComplete [as _callback] (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\Pool.js:99:5) at 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) at Ping.Sequence.OkPacket (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\protocol\sequences\Sequence.js:105:8) at Protocol._parsePacket (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\protocol\Protocol.js:280:23) at Parser.write (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\protocol\Parser.js:73:12) at Protocol.write (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\protocol\Protocol.js:39:16) at Socket. (C:\Users\InWhi\Desktop\ISQA4380\Group-6-Project\node_modules\sails-mysql\node_modules\mysql\lib\Connection.js:96:28) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at readableAddChunk (_stream_readable.js:176:18) at Socket.Readable.push (_stream_readable.js:134:10) at TCP.onread (net.js:548:20) Details: Error: ER_BAD_FIELD_ERROR: Unknown column 'NaN' in 'where clause'

Any suggestions for improvements, and/or solutions to fix my problem, are greatly appreciated!

1
That looks like a mess. SoundCloud.find returns sounds correctly? try to isolate the source of the problemyBrodsky
What is SoundCloud.find ?Sangharsh
SoundCloud is the name of the model, Modelname.find is a built-in method in Sails.js.Winston Smith

1 Answers

1
votes

It seems like you are inadvertently hitting a blueprint route for a model called music when you go to localhost:1337/music/result. It is taking result as the ID in the URL format of /:model/:id. Make sure your route config is set to override that format and point to SoundCloudController.result.