node mysql 返回值,如何从node.js中的mysql SELECT查询返回值

I'm still very new to Node.js, and i'm trying to understand how callbacks work.

So, here is my problem :

I should've put more code :

POST :

app.post('/register', function(req, res) {

//get data from the request

var data = {

username: req.body.username,

email: req.body.email,

password: req.body.password

};

function fetchID(callback) {

connection.query('SELECT id_user FROM USERS WHERE username = ?', data.username, function(err, rows) {

if (err) {

callback(err, null);

} else

callback(null, rows[0].id_user);

});

}

var user_id;

fetchID(function(err, content) {

if (err) {

console.log(err);

return next("Mysql error, check your query");

} else {

user_id = content;

console.log(user_id); //undefined

}

});

console.log(user_id); //undefined

var payload = {

iss: req.hostname,

sub: user_id

}

console.log(payload.sub); //correct id

})

GET :

app.get('/todos', function(req, res) {

if (!req.headers.authorization) {

return res.status(401).send({

message: 'You are not authorized !'

});

}

var token = req.headers.authorization.split(' ')[1];

var payload = jwt.decode(token, "shhh..");

//additional level of security

console.log('sub id is : ' + payload.sub); //undefined

if (!payload.sub) {

return res.status(401).send({

message: 'Authentication failed !'

});

}

})

I commented each console.log to be more clear. I need to get the correct id when i check for if (!payload.sub) in app.get()

解决方案

Your two functions should be something like -

function fetchID(data, callback) {

connection.query('SELECT id_user FROM USERS WHERE username = ?', data.username, function(err, rows) {

if (err) {

callback(err, null);

} else

callback(null, rows[0].id_user);

});

}

and then

var user_id;

fetchID(data, function(err, content) {

if (err) {

console.log(err);

// Do something with your error...

} else {

user_id = content;

}

});

Here in the callback function, the returned variable content will hold the value for user_id.

EDIT

I have not solved the exact problem as you had described above.

But in following example, I have shown that, the callback mechanism is working -

First (Table creation and insert some dummy data)-

use test;

create table users (id int(11) primary key,username varchar(100));

insert into users values(1, "John");

insert into users values(2, "Sham");

Now I have made your post method as get and tested in browser.

Following is the full class tested in my localhost -

var application_root = __dirname,

express = require("express"),

mysql = require('mysql');

var app = express();

var connection = mysql.createConnection({

host : 'localhost',

user : 'root',

password : 'admin',

database: "test"

});

app.get('/getuser', function(req, res) {

//get data from the request

var data = {

username: req.query.username

};

function fetchID(data, callback) {

connection.query('SELECT id FROM users WHERE username = ?',

data.username, function(err, rows) {

if (err) {

callback(err, null);

} else

callback(null, rows[0].id);

});

}

var user_id;

fetchID(data, function(err, content) {

if (err) {

console.log(err);

res.send(err);

// Do something with your error...

} else {

user_id = content;

console.log(user_id);

res.send("user id is -" + user_id);

}

});

})

app.listen(1212);

Now these requests will produce this output -

http://127.0.0.1:1212/getuser?username=john => user id is -1 and

http://127.0.0.1:1212/getuser?username=sham => user id is -2

Hope this code example will help you to understand the callback in node.js.

Thanks

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值