最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - DB query is returning undefined value in node.js with oracledb - Stack Overflow

programmeradmin0浏览0评论

i am new to node.js and javascript and trying to learn the things. in my tests i need to pick a value from Oracle DB through select query and need to use it in to my code later. i am referring the same code given on and it is working fine but am not able to return the result value.

below is my code :

this.getTableData = function(){
        var res;
        oracledb.getConnection(
                {
                    user : "user",
                    password      : "password",
                    connectString : "db " 
                },
                function (err, connection) {
                    if (err) { 
                                console.error(err);
                                console.log("errorrrrrrrrrrr : "+err);
                                return;
                                }               
                    connection.execute("SELECT query",
                         function(err, result) { 
                                    if (err) { 
                                        console.error(err); 
                                            return; 
                                        }
                                    else if(result) {
                                        res = result.rows[0][0];
                                        console.log("result in else if: "+res);
                                        return res;
                                    }});
                    });
    };

the function returns undefined value.

i am new to node.js and javascript and trying to learn the things. in my tests i need to pick a value from Oracle DB through select query and need to use it in to my code later. i am referring the same code given on https://blogs.oracle./opal/entry/introducing_node_oracledb_a_node and it is working fine but am not able to return the result value.

below is my code :

this.getTableData = function(){
        var res;
        oracledb.getConnection(
                {
                    user : "user",
                    password      : "password",
                    connectString : "db " 
                },
                function (err, connection) {
                    if (err) { 
                                console.error(err);
                                console.log("errorrrrrrrrrrr : "+err);
                                return;
                                }               
                    connection.execute("SELECT query",
                         function(err, result) { 
                                    if (err) { 
                                        console.error(err); 
                                            return; 
                                        }
                                    else if(result) {
                                        res = result.rows[0][0];
                                        console.log("result in else if: "+res);
                                        return res;
                                    }});
                    });
    };

the function returns undefined value.

Share Improve this question edited Feb 3, 2016 at 10:26 Jaffer 2,96818 silver badges29 bronze badges asked Feb 3, 2016 at 9:14 Sonia VirkSonia Virk 411 silver badge2 bronze badges 2
  • What's the actual query? What's the value of result, as opposed to result.rows[0][0]? What's the value of result.rows? – Dave Salomon Commented Feb 3, 2016 at 9:22
  • The query is "SELECT OTPCODE FROM (SELECT OTPCODE FROM ONETIMEPASSWORD WHERE MSISDN = 91123456789 Order BY GENERATED_ON DESC) WHERE ROWNUM = 1" and the result.rows & result.rows[0][0] returns as six digit value from table like 123456 – Sonia Virk Commented Feb 3, 2016 at 9:27
Add a ment  | 

1 Answer 1

Reset to default 8

Of course it returns undefined. It's because of async callback functions. You'll need to do something like this:

this.getTableData = function(callback){
    oracledb.getConnection(
        {
            user : "user",
            password      : "password",
            connectString : "db "
        },
        function (err, connection) {
            if (err) {
                console.error(err);
                console.log("errorrrrrrrrrrr : "+err);
                return;
            }
            connection.execute("SELECT query",
                function(err, result) {
                    if (err) {
                        console.error(err);
                        return;
                    }
                    else if(result) {
                        var res = result.rows[0][0];
                        console.log("result in else if: "+res);
                        callback(res);
                    }});
        });
};

getTableData(function (result) {
    console.log(result);
});

The other way you could solve this problem is using a Promise:

this.getTableData = function () {
    return new Promise(function (resolve, reject) {
        oracledb.getConnection(
            {
                user: "user",
                password: "password",
                connectString: "db "
            },
            function (err, connection) {
                if (err) {
                    console.error(err);
                    reject(err);
                    console.log("errorrrrrrrrrrr : " + err);
                    return;
                }
                connection.execute("SELECT query",
                    function (err, result) {
                        if (err) {
                            console.error(err);
                            reject(err);
                            return;
                        }
                        else if (result) {
                            var res = result.rows[0][0];
                            console.log("result in else if: " + res);
                            resolve(res);
                        }
                    });
            });
    });

};

getTableData()
    .then(function (result) {
        console.log(result);
    });

The code you've asked for in your ment:

var AddPage = function () {
    var self = this;
    this.enterOtpInput = element(by.model("beneDetail.otp"));
    this.enterMpinInput = element(by.model("retailerMpin"));
    this.verifyBeneficiaryButton = element(by.xpath("//div[2]/div/button"));
    this.verifyBene = function () {
        support.getTableData()
            .then(function (result) {
                console.log("adam: " + result);
                self.enterOtpInput.sendKeys(result);
                self.enterMpinInput.sendKeys("1111");
                self.verifyBeneficiaryButton.click();
            });

    };
}
发布评论

评论列表(0)

  1. 暂无评论