最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - return node.js-mysql results to a function - Stack Overflow

programmeradmin1浏览0评论

I've got two node.js files: server.js and database.js. I want my socket.io emitting to happen in server.js and the database queries in database.js. Server.js:

// init
io.sockets.on('connection', function(socket) {
    initdb = db.initdb();
    console.log(initdb)
});

My database.js contains basically the following code:

function query(queryString) {
    connection = mysql.createConnection({
        host: '12.23.45.67',
        user: 'user',
        password: 'password',
        database: 'database'
    });

    connection.connect();

    var res = connection.query(queryString, function(err, rows, fields) {
        if (err) throw err;
    });

    connection.end();
}


// export initdb for external usage by server.js
exports.initdb = function() {
    var initdb = query("SELECT * FROM columns;");
};

My problem is that I want the rows object from within the connection.query function to be returned to my initdb function. However the only place where I can log this object is within that function. How can I pass the query results so I can emit the JSON object it from server.js?

I've got two node.js files: server.js and database.js. I want my socket.io emitting to happen in server.js and the database queries in database.js. Server.js:

// init
io.sockets.on('connection', function(socket) {
    initdb = db.initdb();
    console.log(initdb)
});

My database.js contains basically the following code:

function query(queryString) {
    connection = mysql.createConnection({
        host: '12.23.45.67',
        user: 'user',
        password: 'password',
        database: 'database'
    });

    connection.connect();

    var res = connection.query(queryString, function(err, rows, fields) {
        if (err) throw err;
    });

    connection.end();
}


// export initdb for external usage by server.js
exports.initdb = function() {
    var initdb = query("SELECT * FROM columns;");
};

My problem is that I want the rows object from within the connection.query function to be returned to my initdb function. However the only place where I can log this object is within that function. How can I pass the query results so I can emit the JSON object it from server.js?

Share Improve this question asked Jan 14, 2014 at 20:42 DaniDani 2,4888 gold badges30 silver badges44 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Remember that node is asynchronous. So for the most part, you get data back through callbacks rather than as return values to functions.

You'll need to chain a callback through to where your query happens, something like:

// in database.js
exports.initdb = function(cb) {
  query("SELECT * FROM columns", cb)
}

function query(queryString, cb) {
  // .. stuff omitted

  var res = connection.query(queryString, function(err, rows, fields) {
    connection.end();
    if (err) return cb(err);
    cb(null,rows);
  });

// in server.js
io.sockets.on('connection', function(socket) {
  db.initdb(function(err,rows) {
    if (err) {
      // do something with the error
    } else {
      console.log(rows)
    }
  });
});

The callback would be a function taking 2 parameters, err and rows. Your server.js code would need to check the value of err and act accordingly, otherwise it would have the rows.

发布评论

评论列表(0)

  1. 暂无评论