Could you tell me a question about variable assignment in multiple pool.query callbacks in NodeJs?

purpose description:
I want to calculate the total number of pages to be divided based on the total number of data found from mysql and the number of pages per page requested by the front end, so as to make a page number flip machine.

there is a problem:
the total number of pages has been calculated, but there is a problem with the assignment.
uses the first way of writing: pageCount can only get the data when the server is started for the first time, and then the pageCount returned is null, and the returned data object is:
{data: ,
pno:. , / / returned page number
pCount:. , / / the total number of pages, that is, the number of pages I want
pageCount:. . / / what the heck is this and why there are four attributes?
}
but my original data object is like this:
{data:result,pno: ($page+1), pCount:pageCount}
Why is there an extra attribute? This is a little bit.

uses the second way of writing, directly without declaring that all subsequent pageCount of pageCount, will use this.pageCount, to get the desired result data, but I have not assigned an initial value, but where the initial value will print a 1 is what the ghost is. And why can it be printed out in this way, because it is implicitly promoted to the attribute of the object?
if there is a this pointing to the problem, it still doesn"t work to save the this, with a variable, and the result is undefine,. Why?

and is it possible to execute multiple Sql statements like this?
look forward to your advice!

I have considered function promotion and this pointing, but I still can"t figure it out. Why?

router.get ("/ list", (req,res) = > {

)
var obj = req.query;
var $page = Math.round(obj.page);
var $size = Math.round(obj.size);
var $pageCount = null;
// 
var pageCount = 0;//

console.log(obj);
if(!$page){
    $page = 1;
}
if(!$size){
    $size = 10;
}
$page --;
console.log(""+$size); 
console.log(""+ pageCount);//this.pageCount
//console.log(""+ pageCount);//

/ / get the total number of pages

var sqlstr2 = "SELECT * FROM xz_laptop";
pool.query(sqlstr2,(err,result)=>{
    if(err) throw err;
    if(result.length > 0){
        //
        $pageCount = result.length;
        console.log(""+$pageCount);
        console.log($pageCount/$size);
        pageCount = Math.ceil($pageCount/$size);
        console.log(":"+ pageCount);
    }
});

//
var sqlstr = "SELECT * FROM xz_laptop LIMIT ? , ?";
pool.query(sqlstr,[($page*$size),$size],(err, result)=>{
    //console.log()
    if(err) throw err;
    console.log(result);
    if(result.length > 0){
        console.log(""+ pageCount );
        res.send({data:result,pno:($page+1),pCount:pageCount});
        //res.send(result);
    }else{
        res.send({code:301,msg:"list error"});
    }
});
Jun.23,2021
Menu