Closed dileepunni1 closed 10 years ago
taking from https://github.com/felixge/node-mysql
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields)
connection.query has a non-standard callback, function(err, rows, fields) instead of function(err,data)
write a wrapper:
mysqldb.connection.prototype.q = function(sql, callback){
this.query(sql,function(err, rows, fields){
var data = {rows:rows, fields:fields};
callback( err, data);
})
}
then use the wrapper with wait.for. You also need to use wait.forMethod, since it's a method.
$m = wait.forMethod(mysqldb.connection, "q",
"insert into METODICHE_NL(nome,fk_esame,nomeInterno,metodilgia,isAbilitata) values(?,?,?,?,?)",
[nome,fk_esame,nomeInterno,metodilgia,isAbilitata]
),nome,fk_esame,nomeInterno,metodilgia,isAbilitata);
console.log($m);
Also I don't understand your code example. There's a extra ")" ? please paste it indented at least 4 spaces, so github formats it as code.
here is the new code and it works with your suggestions, only one thing i added is connection.prototype = Object.prototype; with out this line i can't add method 'q', always return an error of undefined.
in db.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'newlab',
password : 'new1ab$201311',
database : 'newlab'
});
connection.prototype = Object.prototype;
connection.prototype.q = function(sql, callback){
this.query(sql,function(err, rows, fields){
var data = {rows:rows, fields:fields};
callback( err, data);
});
};
module.exports = {
connection: connection
}
in my controller.js
var mysqldb = require('./db.js');
var wait=require('wait.for');
exports.insert1 = function(req,res){
var nome = req.param('nome');
var fk_esame = req.param('fk_esame');
var nomeInterno = req.param('nomeInterno');
var metodilgia = req.param('metodilgia');
var isAbilitata = req.param('isAbilitata');
var parameters = req.param('paramMethodiche');
$tp = wait.forMethod(mysqldb.connection, "q",
"select * from TIPO_PARAMETRO");
var $sql = "insert into METODICHE_NL(nome,fk_esame,nomeInterno,metodilgia,isAbilitata) values(?,?,?,?,?)";
$sql = mysql.format($sql, [nome,fk_esame,nomeInterno,metodilgia,isAbilitata]);
console.log($sql);
var $mnl = wait.forMethod(mysqldb.connection, "q",$sql);
console.log($mnl);
res.send({data:$tp.rows});
}
in server.js
app.post('/metodichenl1', function(req,res){
wait.launchFiber(routes.metodichenl.insert1, req, res);
});
it is working exactly i wish.
i can insert, but it only return 1, not the data return from callback when use normally, please explain me
code hint $m = wait.for(mysqldb.connection.query("insert into METODICHE_NL(nome,fk_esame,nomeInterno,metodilgia,isAbilitata) values(?,?,?,?,?)", [nome,fk_esame,nomeInterno,metodilgia,isAbilitata] ),nome,fk_esame,nomeInterno,metodilgia,isAbilitata); console.log($m);