简体   繁体   English

如何在nodejs中循环查询mysql? 因为你应该等到查询结果可用否则循环将永远运行

[英]How to query mysql in nodejs in a loop? because you should wait until result of query is available else loop will run forever

In the following code I am generating a uuid and then making sure if it already doesn't exist but the problem is variable 'pid' is not working that is [pid] written after query is not working somehow and I don't why.在下面的代码中,我正在生成一个 uuid,然后确保它是否已经不存在,但问题是变量 'pid' 不起作用,即 [pid] 在查询不起作用后写入的 [pid] 以某种方式不起作用,我不知道为什么。 now plz don't suggest y I am making sure if uuid already does not exist cuz they are unique all the time, just tell me the fact why js variabel var pid = uuid() is not being accepted by mySql connection.query现在请不要建议你我要确保 uuid 是否已经不存在,因为它们一直都是唯一的,只是告诉我为什么 js variabel var pid = uuid() 不被 mySql connection.query 接受的事实
FIRST EDIT: The first connection.query is not even getting executed.一次编辑:第一个 connection.query 甚至没有被执行。
SECOND EDIT: After hours of debugging the problem still remains unsolved but now I know the cause which is not javascript variable but the result of the connection.query which is not readily available so the loop continues to run not waiting for the result.第二次编辑:经过数小时的调试,问题仍未解决,但现在我知道原因不是 javascript 变量,而是 connection.query 的结果,它不容易获得,因此循环继续运行而不是等待结果。 After reading articles I somehow know that promises may do my work but idk how to use them so see ya later until I learn them(also I am changing title of the question since error is not with js variable but with the returning of result of mysql connection.query function).阅读文章后,我不知何故知道承诺可能会完成我的工作,但我知道如何使用它们,所以稍后再看,直到我学习它们(我也在更改问题的标题,因为错误不是 js 变量,而是返回 mysql 的结果connection.query 函数)。
Title changed from:mySQL query is not selecting upon using Javascript variable nodejs标题更改自:mySQL 使用 Javascript 变量 nodejs 时未选择查询
To: above title致:以上标题

//load user model and mysql
const mysql = require('mysql')
const configDB = require('../config/configDB')
const connection = mysql.createConnection(configDB.connection)
//load uuidv4 to generate user ids
const uuid = require('uuid')
module.exports = function(done){
    var exist = true
    do{
        const pid = uuid.v4()
        connection.query("SELECT * FROM PATIENT WHERE PATIENT_ID = ?",[pid], function(err, prows){ 
//query on the above line is not even getting executed and it is because of [pid] but don't know y
                if(err)
                    return done(err)
                if(prows.length){
                   exist = true
                } else {
                    connection.query("SELECT * FROM DOCTOR WHERE DOCTOR_ID = ?",[pid], function(err, drows){
                        if(err)
                            return done(err)
                        if(drows.length){
                            exist = true
                        } else {
                            connection.query("SELECT * FROM ADMIN WHERE ADMIN_ID = ?",[pid], function(err, arows){
                                if(err)
                                    return done(err)
                                if(arows.length){ //corrected from drows to arows after someone answered but the problem is something else and it is still unsolved
                                    exist = true
                                } else {
                                    exist = false
                                    return pid
                                }
                            })
                        }
                    })
                }
            })
    
    
        }while(exist)
    }

In this line of your code在这行代码中

if(drows.length){
    exist = true
}

Seems it should be好像应该

if(arows.length){ //it should be arows.length because arrows.length is never used
    exist = true
}

Earlier my loop was keep generating new pids because all it was doing was generating a new pid then checking if that exist or not but while querying database it was not waiting for the result and since the result was not readily available so the loop was continuing to run till infinity(after debugging via vs code).早些时候,我的循环一直在生成新的 pid,因为它所做的只是生成一个新的 pid,然后检查它是否存在,但是在查询数据库时它没有等待结果,并且由于结果不容易获得,所以循环继续运行到无穷大(通过 vs 代码调试后)。 I used promise to wait for result of loop like this in generateUserId.js file I defined my function as follows我使用 promise 在 generateUserId.js 文件中等待这样的循环结果我定义了我的 function 如下

module.exports = generateUserId

async function generateUserId(){
    let exist = true
    do{
        let pid = uuid.v4()
        const id = await uuidExistOrNot(pid)
        if(id===pid){
            exist = false
            return id
        }
    }while(exist)
}

function uuidExistOrNot(pid){
    return new Promise((resolve, reject) => {
        const selectPQuery = "SELECT * FROM PATIENT WHERE PATIENT_ID = ?"
        const selectDQuery = "SELECT * FROM DOCTOR WHERE DOCTOR_ID = ?"
        const selectAQuery = "SELECT * FROM HADMIN WHERE ADMIN_ID = ?"

        connection.query(selectPQuery, [pid], (err, prows)=>{
            if(!prows.length){
                connection.query(selectDQuery, [pid], (err, drows)=>{
                    if(!drows.length){
                        connection.query(selectAQuery, [pid], (err, arows)=>{
                            if(!arows.length){
                                resolve(pid)
                            } else {
                                reject(err)
                            }
                        })
                    } else {
                        reject(err)
                    }
                })
            } else  {
                reject(err)
            }
        })
    })
}

then when to use it I m importing in that file where I wanna use and then use it as:然后什么时候使用它,我将导入到我想使用的那个文件中,然后将其用作:

generateUserId()
.then(function(id){
 //do what u wanna do with id here that is being generated by generateUserId() 
})
.catch(err=>console.log(err))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM