Node.js和mysql回调:查询回调中的查询 [英] Node.js and mysql Callback : query in query callback

查看:121
本文介绍了Node.js和mysql回调:查询回调中的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的就是在数据库中没有这些数据时插入一些数据,因此我将插入SQL"放入我的Select SQL的回调函数中,但是出现了这样的错误:

All I want to do is insert some data if my database doesn't have that, so I put Insert SQL into my callback function of my Select SQL, but I got error like this:

{[错误:调用退出后无法使查询入队.]代码:"PROTOCOL_ENQUEUE_AFTER_QUIT",致命:假}

{ [Error: Cannot enqueue Query after invoking quit.] code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false }

我的代码段在这里:

db.query('SELECT count(*) as Resultcount FROM tablename WHERE email = ? and password = ?', [post.email, post.password], function(error, result){
    if (result[0].Resultcount == 0){
        var query2 = db.query('INSERT INTO tablename SET ?', [post], function(err, result) {
            if(err){
              console.log(err);
           }
             console.log(result);
          });
    }
    else{
        console.log('have data already');
    }
});

有人可以给我一些建议吗? 谢谢

Could someone give me some advice? Thanks

----更新----

----update----

实际上,select SQL的回调函数不是匿名函数,我关于db.end()的代码段如下:

actually, the callback function of select SQL is not an anonymous function, my code snippet about db.end() is like this:

var QueryResults = new queryResultFuntion(Back_results);

    db.query('SELECT count(*) as Resultcount FROM tablename WHERE email = ? and password = ?', [post.email, post.password], QueryResults.queryResult );


    db.end();

推荐答案

一旦SELECT完成,您的db.end()调用将使连接排队以关闭,因此,当您尝试进行内部INSERT查询时,数据库连接将被关闭,因此错误PROTOCOL_ENQUEUE_AFTER_QUIT,因为您在连接关闭后尝试将新命令排队.

You db.end() call will queue the connection to close once the SELECT has completed, so when you attempt to do the inner INSERT query, the database connection will have been closed, hence the error PROTOCOL_ENQUEUE_AFTER_QUIT, as you are attempting to queue a new command after the connection is closed.

根据创建连接的方式,您应该将db.end()调用移到回调内部,或者如果在程序启动时打开了连接,则根本不使用db.end()调用.

Depending on how you are creating the connection, you should either move your db.end() call inside the callbacks, or not have a db.end() call at all if the connection is opened on program start.

这篇关于Node.js和mysql回调:查询回调中的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆