返回 JavaScript 类的 MySql 结果

huangapple go评论107阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年1月3日 15:37:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574818.html
  • express
  • mysql
  • node.js

在Nunjucks模板中访问数组在 :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定