admin管理员组文章数量:1395329
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 badges1 Answer
Reset to default 6Remember 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
.
本文标签: javascriptreturn nodejsmysql results to a functionStack Overflow
版权声明:本文标题:javascript - return node.js-mysql results to a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744603848a2615232.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论