用jscex改Mysql时返回没得到数据

19 views
Skip to first unread message

周尉

unread,
Jul 31, 2012, 2:07:31 AM7/31/12
to js...@googlegroups.com
不知道是我的写法还是啥原因导致的。

db.queryAsync = Binding.fromStandard(db.query); db.endAsync = Binding.fromStandard(db.end); sql = 'select * from '+opt.db_name+'.'+tb_name+' limit 10'; //db.query(sql,function(err,data){ // console.log(err,data); // db.end(); //}) var testAsync = eval(Jscex.compile("async",function(){ sql = 'select * from '+opt.db_name+'.'+tb_name+' limit 1'; var data = $await(db.queryAsync(sql)); $await(db.endAsync()); console.log(data); })); testAsync().start();

Jeffrey Zhao

unread,
Jul 31, 2012, 2:26:25 AM7/31/12
to js...@googlegroups.com
你用的是哪个MySql驱动,文档哪里能看一下吗?
 
--
 
 
 

周尉

unread,
Jul 31, 2012, 2:30:41 AM7/31/12
to js...@googlegroups.com
用的是这个

  "version": "0.9.6", 


2012/7/31 Jeffrey Zhao <je...@live.com>
--
 
 
 

Jeffrey Zhao

unread,
Jul 31, 2012, 2:47:36 AM7/31/12
to js...@googlegroups.com
看了首页的示例,发现两点:
 
1) connect和end不是异步方法,不用让$await等待。你现在等待了,就永远返回不了了。
2) query返回多个参数,用fromStandard最好指定参数名(你现在只能获取第一个参数了,如果这是你的目的也没问题)。
 
估计就是第1点造成的,记得看文档不要猜,无论是Jscex还是node-mysql的文档……
--
 
 
 

周尉

unread,
Jul 31, 2012, 2:56:15 AM7/31/12
to js...@googlegroups.com
connection那一步我没用这个,end是错了,参数那个我现在是希望能跑通就没加上,我发成这样是对的吗?

db = mysql.createClient({
    host:opt.db_host,
    port:opt.db_port,
    user: opt.db_user,
    password: opt.db_pass,
    debug:false
});
db.queryAsync = Binding.fromStandard(db.query);
sql = 'select * from '+opt.db_name+'.'+tb_name+' limit 10';
//db.query(sql,function(err,data){
//    console.log(err,data);
//    db.end();
//})
var testAsync = eval(Jscex.compile("async",function(){
    sql = 'select * from '+opt.db_name+'.'+tb_name+' limit 1';
    var data = $await(db.queryAsync(sql));
    db.end();
    

--
 
 
 

Jeffrey Zhao

unread,
Jul 31, 2012, 3:08:10 AM7/31/12
to js...@googlegroups.com
看上去似乎没错。还有一些建议,例如db前加var,sql也是,而且没必要散在外面,就作为方法的局部变量就行。
--
 
 
 

周尉

unread,
Jul 31, 2012, 3:25:09 AM7/31/12
to js...@googlegroups.com
我加了个用Task来直接创建的方法,这个可以完成执行,但是输出是先输出Task的结果,后才是data的,我的理解应该是先输出data再会有任务的,请问是啥问题

db.queryAsync = Binding.fromStandard(db.query);
sql = 'select * from '+opt.db_name+'.'+tb_name+' limit 10';
//db.query(sql,function(err,data){
//    console.log(err,data);
//    db.end();
//})
db.queryAsync2 = function(sql){
    return Task.create(function(t){
        db.query(sql,function(err,data){
            t.complete("success",data);
        });
    })
}
var testAsync = eval(Jscex.compile("async",function(){
    var sql = 'select * from '+opt.db_name+'.'+tb_name+' limit 1';
    var data = $await(db.queryAsync2(sql));
    db.end();
    console.log(data);
}));

ret = testAsync().start();
console.log(ret);
在 2012年7月31日 下午3:10,周尉 <chat...@gmail.com>写道:
嗯,但是还是没有完成,我打出
ret = testAsync().start();
console.log(ret);
结果是
{ id: 1,
  _delegate: [Function],
  _listeners: {},
  status: 'running' }
方法里面的data还是没有输出

--
 
 
 


周尉

unread,
Jul 31, 2012, 3:10:56 AM7/31/12
to js...@googlegroups.com
嗯,但是还是没有完成,我打出
ret = testAsync().start();
console.log(ret);
结果是
{ id: 1,
  _delegate: [Function],
  _listeners: {},
  status: 'running' }
方法里面的data还是没有输出

在 2012年7月31日 下午3:08,Jeffrey Zhao <je...@live.com>写道:
--
 
 
 

Jeffrey Zhao

unread,
Jul 31, 2012, 4:11:08 AM7/31/12
to js...@googlegroups.com
你……先看下文档吧,不要让我的心血白流……
 
--
 
 
 

Jeffrey Zhao

unread,
Jul 31, 2012, 4:15:39 AM7/31/12
to js...@googlegroups.com
fromStandard不行,但是你这么手写一个却可以?这个比较奇怪,我晚上检查下fromStandard。
 
什么叫做先输出Task的结果后才是data,你希望它输出什么?ret不是这个异步任务的结果,只是Task对象本身。而且,从你testAsync的写法来看是没有返回值的,要返回值怎么可以没有return?Jscex的语义跟JavaScript完全相同,你要按照普通JavaScript写法,不要用了几天回调,就忘了普通JavaScript函数是怎么写的了呀。
 
还有就是,一定要看看文档……
--
 
 
 

周尉

unread,
Jul 31, 2012, 4:21:01 AM7/31/12
to js...@googlegroups.com
我知道那个ret是task本身,ret.result里面是这个方法的返回值,我意思是刚才那段代码里输出的顺序的问题,

    console.log(data);
这个输出结果在

    console.log(ret);
之后

应该是我对这个理解上还是有问题
--
 
 
 

Jeffrey Zhao

unread,
Jul 31, 2012, 4:23:26 AM7/31/12
to js...@googlegroups.com
这是一定的,这不就是异步编程么。打个比方,就普通的回调:
 
async_call(function () {
    console.log(“A”);
});
 
console.log(“B”);
 
你觉得A和B哪个先输出呢?
--
 
 
 

周尉

unread,
Jul 31, 2012, 4:26:36 AM7/31/12
to js...@googlegroups.com
嗯,理解上是有些问题

--
 
 
 

Jeffrey Zhao

unread,
Jul 31, 2012, 4:31:35 AM7/31/12
to js...@googlegroups.com
没事的,加油,可以先看看文档,看看示例,有问题再来问。
--
 
 
 

Jeffrey Zhao

unread,
Aug 2, 2012, 9:50:57 AM8/2/12
to js...@googlegroups.com
我为fromStandard写了不少单元测试,似乎没有问题,估计还是你写的不对……
 
 
Jeffrey Zhao
Blog: http://blog.zhaojie.me/
--
 
 
 
Reply all
Reply to author
Forward
0 new messages