admin管理员组文章数量:1330591
I am trying get value from database. Trying it out with a demo example. But I am having problem to synchronize the calls, tried using callback function. I am beginner in node.js, so don't know if this is the right way.
FILE 1 : app.js
var data;
var db = require('./db.js');
var query = 'SELECT 1 + 1 AS solution';
var r = db.demo(query, function(result) { data = result; });
console.log( 'Data : ' + data);
FILE 2 : db.js
var mysql = require('./node_modules/mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
});
module.exports.demo = function(queryString, callback) {
try {
connection.connect();
console.log('Step 1');
connection.query(queryString, function(err, rows, fields) {
console.log('Step 2');
if (err) {
console.log("ERROR : " + err);
}
console.log('The solution is: ', rows[0].solution);
callback(rows[0].solution);
return rows[0].solution;
});
callback();
connection.end();
console.log('Step 3');
}
catch(ex) {
console.log("EXCEPTION : " + ex);
}
};
OUTPUT :
Step 1
Step 3
Data : undefined
Step 2
The solution is: 2
Referred to this question also, but it didnt solve my problem : nodeJS return value from callback
I am trying get value from database. Trying it out with a demo example. But I am having problem to synchronize the calls, tried using callback function. I am beginner in node.js, so don't know if this is the right way.
FILE 1 : app.js
var data;
var db = require('./db.js');
var query = 'SELECT 1 + 1 AS solution';
var r = db.demo(query, function(result) { data = result; });
console.log( 'Data : ' + data);
FILE 2 : db.js
var mysql = require('./node_modules/mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root',
});
module.exports.demo = function(queryString, callback) {
try {
connection.connect();
console.log('Step 1');
connection.query(queryString, function(err, rows, fields) {
console.log('Step 2');
if (err) {
console.log("ERROR : " + err);
}
console.log('The solution is: ', rows[0].solution);
callback(rows[0].solution);
return rows[0].solution;
});
callback();
connection.end();
console.log('Step 3');
}
catch(ex) {
console.log("EXCEPTION : " + ex);
}
};
OUTPUT :
Step 1
Step 3
Data : undefined
Step 2
The solution is: 2
Referred to this question also, but it didnt solve my problem : nodeJS return value from callback
Share Improve this question edited May 23, 2017 at 11:53 CommunityBot 11 silver badge asked Mar 26, 2013 at 11:21 Aks1357Aks1357 1,0721 gold badge9 silver badges19 bronze badges2 Answers
Reset to default 3The issue is this:
var r = db.demo(query, function(result) { data = result; });
console.log( 'Data : ' + data);
The console.log
will run before the callback function gets called, because db.demo
is asynchronous, meaning that it might take some time to finish, but all the while the next line of the code, console.log
, will be executed.
If you want to access the results, you need to wait for the callback function to be called:
var r = db.demo(query, function(result) {
console.log( 'Data : ' + result);
});
This is how most code dealing with I/O will function in Node, so it's important to learn about it.
This is my solution
in a file db.js
require('dotenv').config();
const mysql = require('mysql');
const con = mysql.createConnection({
user: process.env.SQL_USER,
host: process.env.SQL_HOST,
database: process.env.SQL_DB,
password: process.env.SQL_PSWD,
port: 3306,
});
async function connect() {
try {
await con.connect();
console.log("Connected to MySql!");
} catch (err) {
console.log(err);
}
}
module.exports = { connect };
Then in another file index.js
const db = require("./data/db");
db.connect();
本文标签: javascriptNodejs MySQL connection query return value to function callStack Overflow
版权声明:本文标题:javascript - Nodejs MySQL connection query return value to function call - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742252802a2441075.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论