使用nodejs和node-mysql返回行 [英] Return rows with nodejs and node-mysql

查看:211
本文介绍了使用nodejs和node-mysql返回行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在发现Nodejs和node-mysql模块.我有一个小问题.我发现的每个教程都说明了如何在数据库上执行选择,但是它们从不返回行,而是始终记录行,这对我来说绝对没有用.

I'm discovering Nodejs and the node-mysql module. I have a small problem. Every tutorial that I find explain how to do a select on the database but they never return the rows, they always log them, which is absolutely useless for my case.

我有一个app.js文件:

I have a app.js file :

// Get continents
app.get("/continents", function(request, result) {
    console.log("Continents : " + database.findAllContinents());
});

还有一个mysql.js文件:

And a mysql.js file :

exports.findAllContinents = function(connection) {
    var connection = getConnection();
    connection.query('select id, code, name from Continent', function (err, rows, fields) {
        if (err) {
            console.log("Error in findAllContinents : " + err)
        }
        return JSON.stringify(rows);
    });
    closeConnection(connection);
};

如何使函数返回行以在app.js文件中使用它们?我真的不想在app.js文件中创建连接,我希望将DAO层分开. 你有什么主意吗?

How can I make the function return the rows to use them in the app.js file ? I don't really want to create connections in the app.js file I want the DAO layer to be separated. Do you have any idea ?

此外,如果有人对使用node-mysql而不是ORM(sequelize,persistence.js ...)的利弊有所了解

Also, if someone has an idea of the pros/cons of using node-mysql instead of an ORM (sequelize, persistence.js...)

谢谢

推荐答案

query()是一个异步函数,您无法从中返回任何结果.因此,任何本身调用异步函数的函数(例如您的findAllContinents)也不能.

query() is an asynchronous function from which you can't return any results. And consequently, any functions which call asynchronous functions themselves (like your findAllContinents) can't either.

相反,您需要传递一个回调函数(也在此处解释),查询完成后将被调用:

Instead, you need to pass a callback function (also explained here) which will be called when the query is done:

// app.js
app.get("/continents", function(request, response) {
  database.findAllContinents(function(err, results) {
    if (err)
      throw err; // or return an error message, or something
    else
      res.send(results); // as a demo, we'll send back the results to the client;
                         // if you pass an object to 'res.send()', it will send
                         // a JSON-response.
  });
});

// mysql.js
exports.findAllContinents = function(cb) {
  var connection = getConnection();
  connection.query('select id, code, name from Continent', function (err, rows, fields) {
    // close connection first
    closeConnection(connection);
    // done: call callback with results
    cb(err, rows);
  });
};

对于(不)使用ORM,这实际上取决于用例.我会选择一个ORM(我最喜欢MySQL的是 patio ),以防我的应用程序需要多个(复杂的) )模型,也许它们之间具有关联.另外,ORM提供的抽象使代码更易于阅读,并且通常可以更轻松地将应用移植到其他数据库.

As for (not) using an ORM, that really depends on the use case. I would choose an ORM (my favorite for MySQL is patio) in case my app requires multiple (complex) models, perhaps with associations between them. Also, the abstraction an ORM provides makes code more easy to read and usually allows to more easily port an app to a different database.

这篇关于使用nodejs和node-mysql返回行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆