2

While printing Query, it just printing "Undefined" but i need the value fetching from the excel to be printed.

this.getQuery = function(excelpath, sheetName, queryName) {
        var query;
        var workbook = new path.Workbook()
        workbook.xlsx.readFile(excelpath)
        .then(function(){
            var sheet = workbook.getWorksheet(sheetName)
            var rowsize=sheet.rowCount
            for(i=1;i<rowsize;i++){
                var Row = sheet.getRow(i)
                var qName = Row.getCell(1).value
                if(qName==queryName){
                    query=Row.getCell(2).value
                    break;
                }
            }
        })
        return query;
    };
Venugopal P
  • 217
  • 1
  • 2
  • 9
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Klaycon Jul 22 '20 at 16:54

2 Answers2

0

You're not setting the value of query until the callback you supply to then is executed, which will actually be temporally after the return query line. Reading from a file is likely to take non-negligible time, and the method you're using won't block the call stack in the interim, so the subsequent instructions will still be executed in the meantime and your function will run to completion, returning undefined before the callback code even starts.

If you're using an asynchronous method to read the file, you need to write an asynchronous function to use it and return the result - either by returning a Promise or taking and calling a callback.

This is a decent resource to explain how and why: https://www.pluralsight.com/guides/front-end-javascript/introduction-to-asynchronous-javascript

richsilv
  • 7,993
  • 1
  • 23
  • 29
0

It happens async and query is returned before the promise of readFile(excelpath) is resolved. You can either wait till the Promise is resolved And chain the return statement OR return the promise directly and Resolve in test spec.

Method 1: Return the query value from inside the resolved promise. So in your test spec resolve the Promise for the actual value

this.getQuery = function(excelpath, sheetName, queryName) {
    var query;
    var workbook = new path.Workbook()
    return workbook.xlsx.readFile(excelpath)
        .then(function(){
            var sheet = workbook.getWorksheet(sheetName)
            var rowsize=sheet.rowCount
            for(i=1;i<rowsize;i++){
                var Row = sheet.getRow(i)
                var qName = Row.getCell(1).value
                if(qName===queryName){
                    query=Row.getCell(2).value
                    return query;
                }
            }
        })
};

Method 2: Chain the line - return query to be trigerred after the readFile() promise is resolved and and the comparison is done.

this.getQuery = function(excelpath, sheetName, queryName) {
    var query;
    var workbook = new path.Workbook()
    workbook.xlsx.readFile(excelpath)
        .then(function(){
            var sheet = workbook.getWorksheet(sheetName)
            var rowsize=sheet.rowCount
            for(i=1;i<rowsize;i++){
                var Row = sheet.getRow(i)
                var qName = Row.getCell(1).value
                if(qName===queryName){
                    query=Row.getCell(2).value
                    break;
                }
            }
         // Now Chain the logic of returning the value to this then
        }).then(function _returnValueOnlyAfterValueAssigned() {
        return query;
    })
};
AdityaReddy
  • 3,625
  • 12
  • 25
  • Method 1, prints as "[object Promise]". Method 2 printing "Undefined" again. – Venugopal P Mar 13 '17 at 11:42
  • Method 1 will return you a promise and not a value. To get the value from a promise you need to resolve it using `.then()` method. In your case, you should use it like, `this. getQuery("path","sheet1","somequery").then(function(returnValueFromgetQueryMethod){ console.log(returnValueFromgetQueryMethod); })` – Sudharsan Selvaraj Mar 13 '17 at 11:50
  • Can you update the question with sample contents of your Excel file and sample spec where you call this method – AdityaReddy Mar 13 '17 at 11:55
  • Regarding method1 - Please follow @SudharsanSelvaraj 's advice .. As i have stated in the answer, There are two ways - 1. Either return a promise thats resolves to the value or 2. resolve the Promise here and then chain the return statement... Method 1 requires to follow Sudharsan 's advice and resolve the promise – AdityaReddy Mar 13 '17 at 11:59
  • @AdityaReddy Even method 2 will return a promise and we need to explicitly resolve it using `then()`. I guess using Method 1 is pretty simple and straight forward. – Sudharsan Selvaraj Mar 13 '17 at 12:03
  • @Sudharsan .. method 2 doesnt return a promise right ... i am travelling now and couldn't check but i will check once possible – AdityaReddy Mar 13 '17 at 12:27