Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DB query is returning undefined value in node.js with oracledb

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.com/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.

like image 288
Sonia Virk Avatar asked Feb 08 '23 20:02

Sonia Virk


1 Answers

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 comment:

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();
            });

    };
}
like image 124
Adam Avatar answered Feb 10 '23 11:02

Adam