Pipe2Path
Pipe2Path

Reputation: 561

Querying MySql table using Sails.js

I am new to Sails.js. I followed much of the documentation, but am not able to retrieve simple data from my MySql database. When I run the route in my browser - http://localhost:1337/getAllChoices, the browser seems to be hang. I don't see any errors in the browser console either.

In my models folder, I have a Multiplechoice.js

module.exports = {
    attributes: {
        multiplechoiceid: { type: 'integer'},
        description: { type: 'string'}
    },

    getAllChoices: function(){
        return this.description ;
    }
}

My route.js contains this: 'get /allchoices': 'HomeController.GetAllChoices'

My HomeController contains this: getallchoices: function(req, res){ return MultipleChoice.getAllChoices(); }

Am I missing something?

Thank you.

Upvotes: 1

Views: 311

Answers (1)

Alex Alksne
Alex Alksne

Reputation: 518

It looks like you're trying to retrieve the description for each multiple choice. To do that you'd need the following code:

getallchoices: function(req, res){

  var descriptions = [];

  MultipleChoice.find({}).exec(function findCB(err, choices)
  {
    for(var i in choices)
    {
      descriptions.push(choices[i].description);
    }

    res.json(200, descriptions);
  });

}

getAllChoices() will not retrieve data for each record in your MultipleChoice model. Also the reason the server hangs is because it's waiting for you to specify a response to send back to the client (return "someValue"; does not accomplish this). Since you don't provide one it just waits forever. Try commenting out res.json(200, descriptions); and the server will similarly hang forever.

Upvotes: 1

Related Questions