[英]Callback function returning undefined value when calling it from function in another file
I'm new to javascript/nodeJS programming. 我是javascript / nodeJS编程的新手。 I want to read a cell value from excel file which I am able to do using excelJS API and I need to return this cell value to a function in anothor file. 我想从excel文件中读取一个单元格值,我可以使用excelJS API来完成,我需要将此单元格值返回给anothor文件中的函数。 since it's an asynchronous operation I am using callbacks to handle it but somehow callback function is returning undefined value. 因为它是一个异步操作,我使用回调来处理它,但不知何故回调函数返回未定义的值。 I am unable to fix it even after trying so hard. 即使在努力之后我也无法修复它。 It would be a great help for me If someone provides me with a solution. 如果有人为我提供解决方案,对我来说将是一个很大的帮助。 I have two .js files, first is readvalues.js file and other is mycallback.js file. 我有两个.js文件,第一个是readvalues.js文件,另一个是mycallback.js文件。 I am calling mycallback function from readvalue function which is in another file with three parameters(first is sheetName, second is ColumnName, third is callback function). 我从readvalue函数调用mycallback函数,该函数在另一个带有三个参数的文件中(第一个是sheetName,第二个是ColumnName,第三个是回调函数)。 when I print the value received from mycallback function it is showing undefined. 当我打印从mycallback函数收到的值时,它显示未定义。 Below is the code snippet: 以下是代码段:
readvalus.js readvalus.js
var mycallback = require('./mycallback.js');
var Excel = require('exceljs');
function readvalues(){
console.log('Calling mycallback Function');
mycallback('General Data', 'Name', function(name){
console.log('Received name from mycallback function = ' +name); //displays value of name as undefined
});
console.log('Finished calling mycallback Function');
}
readvalues();
mycallback function (which internally calls another function called getData). mycallback函数(内部调用另一个名为getData的函数)。
var Excel = require('exceljs');
var mycallback = function(sheetName, columnName,callbackOne) {
getData(sheetName, columnName, function(value){
console.log('Data received from getData Function = '+value); //prints value as undefined.
callbackOne(value);
});
}
function getData(sheetName, columnName, callbackTwo){
var workbook = new Excel.Workbook();
var filename = './data.xlsx';
workbook.xlsx.readFile(filename) //function of exceljs API
.then(function(){
var worksheet = workbook.getWorksheet(sheetName);
var row = worksheet.getRow(1).values;
for(var i=1; i<row.length; i++){
if(row[i] === columnName){
var rowVal = worksheet.getRow(2).values;
var data = rowVal[i];
}
}
console.log('Data passed from getData Function = '+data); //prints value of data as undefined.
callbackTwo(data);
})
}
module.exports = mycallback;
Note: When I run mycallback function (like mycallback('Common Data', 'Name') without callbackOne in mycallback function) individually it is printing expected value "John" perfectly. 注意:当我在mycallback函数中运行mycallback函数(如mycallback('Common Data','Name')而没有callbackOne)时,它会完美地打印期望值“John”。
Outputs: 输出:
Excel Data Data which I want to read from the excel 我想从Excel中读取的 Excel数据 数据
将“常规数据”替换为readvalus.js中的“公共数据”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.