【问题标题】:How to store Node mysql query in a variable?如何将Node mysql查询存储在变量中?
【发布时间】:2018-12-19 04:57:24
【问题描述】:

我正在使用节点 mysql 连接到外部数据库,我可以 console.log 查询结果,但是当我尝试返回数据时,我得到 Promise pending

这里是函数 -

export function get_info() {

    var mysql = require('mysql');
    var connection = mysql.createConnection({
        host: 'xxxxxx',
        user: 'bob',
        port: 'xxxx',
        password: 'bob',
        database: 'li_webform'
    });

    connection.connect(function (err) {
        if (err) throw err;
        connection.query("SELECT Name FROM webform", function (err, result) {
            if (err) throw err;
            //console.log(result); **This works**
            //console.log(result[0].Name);
            return result;
        });
    });
}

我正在尝试像这样设置一个等于返回值的变量

var newVar = get_info();
console.log(newVar);

但我收到了承诺待处理通知。我相信这与 JavaScript 的异步特性有关。我已尝试关注此帖子 How to properly return a result from mysql with Node?,但无法正常工作。

这是我在该帖子之后尝试的内容

export function get_info(callback) {

    var mysql = require('mysql');
    var connection = mysql.createConnection({
        host: 'xxxxx',
        user: 'bob',
        port: 'xxxxxx',
        password: 'bob',
        database: 'li_webform'
    });

    connection.connect(function (err) {
        if (err) throw err;
        connection.query("SELECT Name FROM webform", function (err, result) {
            if (err) throw err;
            //console.log(result);
            //console.log(result[0].Name);
            return callback(result);
        });
    });
}

然后我尝试通过

var stuff_i_want = '';

get_info(function (result) {
    stuff_i_want = result;
    console.log(stuff_i_want);
});

但我没有得到任何输出

编辑**这是新的尝试

export function get_info() {

    var mysql = require('mysql');
    var connection = mysql.createConnection({
        host: 'xxxx',
        user: 'bob',
        port: 'xxxxxx',
        password: 'bob',
        database: 'li_webform'
    });

    connection.connect(function (err) {
        if (err) throw err;
        connection.query("SELECT Name FROM webform", function (err, result) {
            if (err) throw err;
            //console.log(result);
            //console.log(result[0].Name);
            return result;
        });
    });
}

第二部分

    get_info().then((callbackData)=>{
   console.log("Data: ", callbackData)
 }).catch((error)=>{
    console.log("Error", error)
 });

【问题讨论】:

    标签: javascript mysql node.js asynchronous


    【解决方案1】:

    函数返回一个承诺。你可以这样处理:

     get_info().then((callbackData)=>{
       console.log("Data: ", callbackData)
     }).catch((error)=>{
        console.log("Error", error)
     });
    

    【讨论】:

    • 我收到数据:未定义。 get_info函数里还有return语句吗?
    • 你是尝试第一种还是第二种方式?为此,您应该使用您在帖子中编写的第一段代码。
    • 我使用了第一种方式,我得到了 Promise Object
    • 使用我发布的解决方案?如果函数返回一个承诺,那应该可以工作。
    • 是的,您发布了解决方案。我刚刚尝试更新了我的原始帖子。它导致数据未定义。你有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2016-05-06
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    相关资源
    最近更新 更多