简体   繁体   English

NodeJS - module.export & fileRead 值返回给调用者 Class

[英]NodeJS - module.export & fileRead value return to Caller Class

I have readmsgfile.js file which has module.exports and fs.readFile function in it which reads a File and returns the data after some operation.我有readmsgfile.js文件,其中包含module.exports和 fs.readFile function 读取文件并在某些操作后返回数据。

I would like to return the msgBody from readmsgfile class but the Caller Class A always gets "Undefined" in the line code var respondMsg .我想从readmsgfile class 返回msgBody但调用者 Class A 总是在行代码 var respondMsg中得到“未定义”。 I tried to return the value outside of fs.readfile scope but did not work.我试图返回fs.readfile scope 之外的值,但没有成功。

what modifications have to be done to get the msgBody var value from readmsgfile file to caller Class A必须进行哪些修改才能从readmsgfile文件中获取msgBody var 值到调用者 Class A

readmsgfile.js
--------------------
const fs = require("fs");

var msgBody;

module.exports = {
  readMSGFile: async function (filepath) {

   let path = filepath;
    var regex = /\\/g;
    const pathOfString = path.replace(regex, "/");

      fs.readFile(pathOfString, function (err, data) {
      var msgBody = Sum Operation Done! ;
      return msgBody;
    });

   return msgBody;
  },
  
};


Class A:
--------------------------
const readFile = require("../readmsgfile");

Async function(){
var respondMsg = await readFile.readMSGFile(detailsoffile);
console.log(respondMsg);
}

You should not mix callbacks and async functions up:你不应该混合回调和异步函数:

Promise way: Promise方式:

//readmsgfile.js
module.exports = {
  readMSGFile: function (filepath) {
    // returning the Promise
    return new Promise((resolve, reject) => {
      let path = filepath;
      var regex = /\\/g;
      const pathOfString = path.replace(regex, "/");
   
      fs.readFile(pathOfString, function(err, data) { // this is a callback function!
        var msgBody = Sum Operation Done! ;
        
        // here you could also check if there is a error       
        if (err) { reject(err); }

        // return msgBody;
        // You can not return from the callback so use resolve
        resolve(msgBody);
      });
    });
  }
};

//Class A:
async function myFun(){
  var respondMsg = await readFile.readMSGFile(detailsoffile);
  console.log(respondMsg);
}

Callback way:回调方式:

//readmsgfile.js
module.exports = {
  readMSGFile: function (filepath, successCallback, failureCallback) {
    let path = filepath;
    var regex = /\\/g;
    const pathOfString = path.replace(regex, "/");
   
    fs.readFile(pathOfString, function (err, data) { // this is a callback function!
      var msgBody = Sum Operation Done! ;

        // here you could also check if there is a error       
        if (err) { failureCallback(err); }

        // return msgBody;
        // You can not return from the callback so use your successCallback
        successCallback(msgBody);
    });
  }
};

//Class A:
function myFun(){
  var respondMsg = readFile.readMSGFile(detailsoffile, msgBody => {
    console.log(respondMsg);
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM