使用Sails.js查询MySql表

我是Sails.js的新手。 我遵循了大部分文档,但无法从MySql数据库中检索简单的数据。 当我在浏览器中运行路由器时 – http:// localhost:1337 / getAllChoices ,浏览器似乎挂起。 我在浏览器控制台中也看不到任何错误。

在我的模型文件夹中,我有一个Multiplechoice.js

module.exports = { attributes: { multiplechoiceid: { type: 'integer'}, description: { type: 'string'} }, getAllChoices: function(){ return this.description ; } } 

我的route.js包含这个:'get / allchoices':'HomeController.GetAllChoices'

我的HomeController包含这个:getallchoices:function(req,res){return MultipleChoice.getAllChoices(); }

我错过了什么吗?

谢谢。

它看起来像你试图检索每个多项select的描述。 要做到这一点,你需要下面的代码:

 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()将不会检索您的MultipleChoice模型中的每个logging的数据。 此外,服务器挂起的原因是因为它正在等待你指定一个响应发送回客户端( return "someValue";不完成这一点)。 既然你不提供一个,它只是永远等待。 尝试注释res.json(200, descriptions); 而服务器也会永远挂起。