gpt4 book ai didi

mysql - 使用 Sails.js 查询 MySql 表

转载 作者:行者123 更新时间:2023-11-29 00:00:54 25 4
gpt4 key购买 nike

我是 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:函数(请求,资源){ 返回 MultipleChoice.getAllChoices();

我错过了什么吗?

谢谢。

最佳答案

看起来您正在尝试检索每个多项选择的描述。为此,您需要以下代码:

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 模型中的每条记录检索数据。服务器挂起的原因也是因为它在等待您指定一个响应以发送回客户端(return "someValue"; 不会完成此操作)。由于您不提供它,因此它只会永远等待。尝试注释掉 res.json(200, descriptions);,服务器同样会永远挂起。

关于mysql - 使用 Sails.js 查询 MySql 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29927519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com