<tfoot id='qI28x'></tfoot><legend id='qI28x'><style id='qI28x'><dir id='qI28x'><q id='qI28x'></q></dir></style></legend>

    • <bdo id='qI28x'></bdo><ul id='qI28x'></ul>

    <small id='qI28x'></small><noframes id='qI28x'>

      <i id='qI28x'><tr id='qI28x'><dt id='qI28x'><q id='qI28x'><span id='qI28x'><b id='qI28x'><form id='qI28x'><ins id='qI28x'></ins><ul id='qI28x'></ul><sub id='qI28x'></sub></form><legend id='qI28x'></legend><bdo id='qI28x'><pre id='qI28x'><center id='qI28x'></center></pre></bdo></b><th id='qI28x'></th></span></q></dt></tr></i><div id='qI28x'><tfoot id='qI28x'></tfoot><dl id='qI28x'><fieldset id='qI28x'></fieldset></dl></div>
    1. 如何在节点 js mysql 查询函数中找到匿名函数之外

      时间:2023-06-06
    2. <i id='PUWV4'><tr id='PUWV4'><dt id='PUWV4'><q id='PUWV4'><span id='PUWV4'><b id='PUWV4'><form id='PUWV4'><ins id='PUWV4'></ins><ul id='PUWV4'></ul><sub id='PUWV4'></sub></form><legend id='PUWV4'></legend><bdo id='PUWV4'><pre id='PUWV4'><center id='PUWV4'></center></pre></bdo></b><th id='PUWV4'></th></span></q></dt></tr></i><div id='PUWV4'><tfoot id='PUWV4'></tfoot><dl id='PUWV4'><fieldset id='PUWV4'></fieldset></dl></div>
        <tbody id='PUWV4'></tbody>

        <legend id='PUWV4'><style id='PUWV4'><dir id='PUWV4'><q id='PUWV4'></q></dir></style></legend>

        <small id='PUWV4'></small><noframes id='PUWV4'>

        • <bdo id='PUWV4'></bdo><ul id='PUWV4'></ul>

            <tfoot id='PUWV4'></tfoot>
              1. 本文介绍了如何在节点 js mysql 查询函数中找到匿名函数之外的返回变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                你好朋友,我是 node js 的新手,我们如何获取 mysql 查询匿名函数中使用的变量值?

                Hello friend i am new in node js, how we can get variable value used in mysql query anonymous function ?

                var alldata = function(){
                
                var http = require('http'), mysql = require('mysql');
                
                var client = mysql.createConnection({
                       host: '127.0.0.1',
                   user: 'root',
                   password: ''
                });
                
                client.connect();
                client.query("use cakephp2");
                
                client.query("SELECT id, title,body,created from posts", 
                        function(err, results, fields) {
                            if (err) throw err;
                
                            var output = '<h1>Latest Posts</h1><ul><table border=1><tr>';
                            for (var index in fields) {
                                output += '<td>' + fields[index].name + '</td>';
                            }
                            output += '</tr>';
                            for (var index in results) {
                                output += '<tr><td>' + results[index].id + '</td>';
                                output += '<td>' + results[index].title + '</td>';
                                output += '<td>' + results[index].body + '</td>';
                                output += '<td>' + results[index].created + '</td></tr>';
                            }
                            output += '</ul>';
                            // console.log(output);
                            // return output;
                
                        }
                    ); 
                  return  output ;
                }
                exports.alldatas = alldata();
                

                在上面的代码中,当使用console.log(output)给出正确的结果时,在client.query中我没有发现返回输出结果,但不能访问匿名函数之外的输出值.

                in above code i did not found return output result while in client.query when use console.log(output) give correct result, but can not access output value outside of anonymous function.

                请帮帮我

                提前致谢.

                推荐答案

                您将无法在回调函数之外访问该变量.原因是,Node.js 有一个特殊功能,即在执行异步 IO 任务(在您的情况下为 mysql 查询)后,将回调函数作为下一个要执行的代码块传递.

                You won't be able to access that variable outside the callback function. The reason is, the Node.js has a special feature of passing a callback function as the next block of code to be executed after performing an asynchronous IO task, (in your case a mysql query).

                当您的程序进入 IO 模式时,您在回调函数之后编写的代码会立即执行.并且 output 变量直到回调被触发才准备好.因此您无法访问它.

                The code you write after the callback function gets executed immediately when your program goes into IO mode. And the output variable is not ready untill the callback is fired. and hence you can not access it.

                您可以在此处

                您必须在该回调函数中使用 output 或在那里调用其他函数并将 output 作为参数传递给它.

                You will have to ue the output within that callback function or call some other function there and pass output to it as a parameter.

                这篇关于如何在节点 js mysql 查询函数中找到匿名函数之外的返回变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:具有多个索引的 Doctrine 2 下一篇:在 ubuntu 上使用 ansible 安装 MySQL

                相关文章

                最新文章

              2. <tfoot id='aQnu1'></tfoot>

                1. <small id='aQnu1'></small><noframes id='aQnu1'>

                  • <bdo id='aQnu1'></bdo><ul id='aQnu1'></ul>
                  <legend id='aQnu1'><style id='aQnu1'><dir id='aQnu1'><q id='aQnu1'></q></dir></style></legend>

                    <i id='aQnu1'><tr id='aQnu1'><dt id='aQnu1'><q id='aQnu1'><span id='aQnu1'><b id='aQnu1'><form id='aQnu1'><ins id='aQnu1'></ins><ul id='aQnu1'></ul><sub id='aQnu1'></sub></form><legend id='aQnu1'></legend><bdo id='aQnu1'><pre id='aQnu1'><center id='aQnu1'></center></pre></bdo></b><th id='aQnu1'></th></span></q></dt></tr></i><div id='aQnu1'><tfoot id='aQnu1'></tfoot><dl id='aQnu1'><fieldset id='aQnu1'></fieldset></dl></div>