currently,i learn node-mysql in node.js,but I meet a question,try a lot,i don't how to solve it ?
like this:
var express = require('express');
var app = express();
var mysqlcon=require('./testmysql');
var mysqlconn=mysqlcon.conn;
var http=require("http");
var url=require("url");
app.use('/sql',testExist).listen(3000);
function testExist(req,res,next){
var query = url.parse(req.url,true).query;
var name=query.name;
mysqlconn.connect();
var result=function(err, results){
return results.length;
}
mysqlconn.query('select * from user where name = ?',[name],result);
mysqlconn.end();
if(result>1)
console.log('result:'+result)
res.end("name is already exist!")
}
I want to get the inner function's result in outer function ,for example the length of such query will be 1(results.length==1),but i want to use the result in testExist(),we know that node use callback function,so that i can't get the inner result in outer function. So can any guys help me to solve it.
you need move the using code inside the callback.
function callback(err, results) {
if(results.length >1) console.log('result:' + results.length);
res.end("name is already exists!");
}
mysqlconn.query("select * from user where name= ?", [name], callback);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.