英文:
Return MySql results from JavaScript Class
问题
我在使用Express JS模型返回结果时遇到了问题。我认为我的问题在于对回调和JS类的理解不够。我似乎无法让它工作。
我有以下代码。
从server.js中,我有这个路由:
app.get('/api/v1/loralocations/:id', LoraLocation.getOne);
这调用下面的控制器动作:
getOne(req, res) {
const lora_location = LoraLocationModel.findOne(req.params.id);
if (!lora_location) {
return res.status(404).send({'message': 'LoraLocation not found'});
}
return res.status(200).send(lora_location);
},
模型:
var pool = require('./mysqlDb.js');
import moment from 'moment';
class LoraLocation {
/**
* class constructor
* @param {object} data
*/
constructor() {
}
findOne(id) {
var sql = ('SELECT test FROM Test WHERE id = ?');
pool.query(sql, [id], function (err, results, fields) {
if (err) return err;
console.log(results); //works
return results; //nope
});
}
}
控制台输出结果,但没有返回到控制器。我已经查看了许多帖子,但似乎找不到解决方法。我认为控制器中可能也需要一个回调?
这是否是一个简单API项目的标准模式?
谢谢
英文:
I am having an issue returning results from a Express JS model. I think my problem is a lack of understanding of callbacks and JS Classes. I just can't seem to make it work.
I have the following code.
From the server.js I have this route:
app.get('/api/v1/loralocations/:id', LoraLocation.getOne);
Which calls the Controller action below
getOne(req, res) {
const lora_location = LoraLocationModel.findOne(req.params.id);
if (!lora_location) {
return res.status(404).send({'message': 'LoraLocation not found'});
}
return res.status(200).send(lora_location);
},
.
.
.
Model
var pool = require('./mysqlDb.js');
import moment from 'moment';
class LoraLocation {
/**
* class constructor
* @param {object} data
*/
constructor() {
}
findOne(id) {
var sql = ('SELECT test FROM Test WHERE id = ?');
pool.query(sql, [id], function (err, results, fields) {
if (err) return err;
console.log(results); //works
return results; //nope
});
}
The console.log returns the results, but nothing is returned to the controller. I have gone through a number of posts but can't seem to find a solution. I think maybe I need a callback in the controller also?
Would this be a fairly standard pattern for a simple API project?
Thanks
答案1
得分: 0
你正在不正确地使用回调。要么你可以使用async/await,这是一个不错的选择,要么如果你想坚持使用回调,那么你需要返回回调函数。
控制器
const lora_location = LoraLocationModel.findOne(req.params.id, function (err, data) => {
});
在模型中应该是这样的
findOne(id, cb) {
var sql = ('SELECT test FROM Test WHERE id = ?');
pool.query(sql, [id], function (err, results, fields) {
if (err) cb(err, []);
console.log(results); //works
cb(null, results);
});
}
你可以在这个链接中获取有关回调的更多详细信息
https://medium.com/better-programming/callbacks-in-node-js-how-why-when-ac293f0403ca
英文:
You are using callback incorrectly.
either you can use async/await which is a good option or if you want to stick with callbacks then you need to return callback function.
controllers
const lora_location = LoraLocationModel.findOne(req.params.id, function (err, data) => {
});
in the model it should be
findOne(id, cb) {
var sql = ('SELECT test FROM Test WHERE id = ?');
pool.query(sql, [id], function (err, results, fields) {
if (err) cb(err, []);
console.log(results); //works
cb(null, results);
});
}
you can get more details for callbacks in this link
https://medium.com/better-programming/callbacks-in-node-js-how-why-when-ac293f0403ca
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论