简体   繁体   English

howto节点module.exports

[英]howto Node module.exports

I want to separate the code for executing mysql query in Node, so I am trying to use the Revealing Module pattern here 我想分离用于在Node中执行mysql查询的代码,因此我尝试在此处使用“显示模块”模式

/* pool  -create connection pool mysql*/
var sqlQuery = function (sqlString) {
    var _result = pool.getConnection(function (err, connection) {
/* error handling*/
    connection.query(sqlString, function (err, rows) {
        console.log(this.sql);
        if (!err) {
           return _result = rows; <============
        } 
        connection.release();
    });
    return;
    });
    console.log(_result);
    return { recordSet : _result }
};
module.exports = sqlQuery;

How can I return rows to my app.js. 如何将行返回到我的app.js。 the code below for calling sqlQuery is not working 以下用于调用sqlQuery的代码不起作用

var SqlQuery = require(./path/to/sqlQueryFile);
var rows = SqlQuery('pass sql here').recordSet;
console.log(row);
res.json(rows);

Your code is asynchronous, but you're calling it synchronously. 您的代码是异步的,但是您正在同步调用它。

If you wanted to do it like this, you'll also need to pass a callback to SqlQuery . 如果您想这样做,还需要将回调传递给SqlQuery

/* pool  -create connection pool mysql*/
var sqlQuery = function (sqlString, callback) {
    var _result = pool.getConnection(function (err, connection) {
    /* error handling*/
      connection.query(sqlString, function (err, rows) {
        console.log(this.sql);
        if (!err) {
          callback(rows);
        } 
        connection.release();
      });
  });
};
module.exports = sqlQuery;

And then call it with: 然后调用:

var SqlQuery = require(./path/to/sqlQueryFile);
var rows = SqlQuery('pass sql here', function(recordSet){
  console.log(recordSet);
  res.json(recordSet);
});

Edit: If you're using newer versions of JavaScript, you have a few more options. 编辑:如果您使用的是JavaScript的较新版本,则还有更多选择。

If you have access to Promises, you can do this: 如果您可以访问Promises,则可以执行以下操作:

function sqlQuery (sqlString) {
  return new Promise((resolve, reject) => {
    pool.getConnection(function (err, connection) {
      if (err) { return reject(err); } // error handling
      connection.query(sqlString, function (err, rows) {
        if (err) { return reject(err); }
        resolve(rows);
        connection.release();
      });
    });
  });
}
module.exports = sqlQuery;

And then you'd use it like: 然后您可以像这样使用它:

var SqlQuery = require(./path/to/sqlQueryFile);
SqlQuery('pass sql here')
  .then(function(recordSet) {
    console.log(recordSet);
    res.json(recordSet);
  })
  .catch(function(err) {
    // do your error handling
    res.status(500).json({ err: 'Sorry there was an error' });
  });

If you're using even newer JavaScript, you can use the async/await syntax (currently available via Babel, and I think in FireFox. Chrome in V55). 如果您使用的是更新的 JavaScript,则可以使用async / await语法(当前可通过Babel使用,我认为在FireFox中可用。在V55中为Chrome)。

var SqlQuery = require(./path/to/sqlQueryFile);
async handleQuery(query) {
  try {
      var rows = await SqlQuery(query);
      res.json(rows);
  } catch (e) {
    console.log('Error!', e);
  }
}

To chain multiple queries together: 要将多个查询链接在一起:

async handleQuery(query) {
  try {
      return await SqlQuery(query);
  } catch (e) {
    console.log('Error!', e);
  }
}

var rows = await handleQuery('select * from tablename');
var rowsToReturn = await handleQuery('select id from another_table where name = "' + rows[0].name + '"');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM