1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.