【问题标题】:Returning data from js function not arriving in calling function从js函数返回数据未到达调用函数
【发布时间】:2017-01-29 00:48:12
【问题描述】:

我正在关注 Electron 文件系统教程:

How to choose , read, save, delete or create a file with Electron Framework

其中包括以下代码以打开加载文件对话框:

        document.getElementById('select-file').addEventListener('click',function(){
            dialog.showOpenDialog(function (fileNames) {
                if(fileNames === undefined){
                    console.log("No file selected");
                }else{
                    document.getElementById("actual-file").value = fileNames[0];
                    readFile(fileNames[0]);
                    // myData = readFile(fileNames[0]);  <--- 
                }
            }); 
        },false);

以及下面的代码来实际读取文件:

        function readFile(filepath) {
            fs.readFile(filepath, 'utf-8', function (err, data) {
                if(err){
                    alert("An error ocurred reading the file :" + err.message);
                    return;
                }

                document.getElementById("content-editor").value = data;
            });
        }

如您所见,最后一行使用导入的数据更新了 DOM。但是,我想将数据返回给我的调用函数,因为它不是 HTML。请注意第一个片段末尾的注释行。这就是我想做的事情。

实际上是 XML 必须被处理而不是转储到 DOM 中。但是我所有返回数据的尝试(特别是“返回数据”,无论是最后一行还是最后一个花括号之前)都失败了。两者都不会导致调用函数获取数据。

调用函数是否可能需要等待这个完成?或者,更有可能的是,我只是没有从这种类型的函数中正确返回数据。非常感谢任何帮助。

【问题讨论】:

    标签: javascript electron


    【解决方案1】:

    由于您的readFile(filepath) 正在执行异步文件读取操作,因此您不能在此处使用简单的返回。但是,您可以将回调传递给您的 readFile 函数,并在您的数据读取操作完成时稍后调用它。像这样。

    创建回调

    function fileReadComplete(data) {
        myData = data;
        // Do whatever you want
    }
    

    并将其称为 .

    readFile(fileNames[0], fileReadComplete);
    

    更改你的 readfile 函数:

    function readFile(filepath, callback) {
            fs.readFile(filepath, 'utf-8', function (err, data) {
                if(err){
                    alert("An error ocurred reading the file :" + err.message);
                    return;
                }
                callback(data);
                document.getElementById("content-editor").value = data;
            });
        }
    

    【讨论】:

    • 这很好用。这样我也可以从 ctrl-L 以及 File 菜单中调用它,然后右键单击背景。这应该让我做所有这些。谢谢。
    【解决方案2】:

    据我了解,这应该可以满足您的要求。

    // 更新;由“返回”改为回调函数。

        document.getElementById('select-file').addEventListener('click',function(){
            dialog.showOpenDialog(function (fileNames) {
                if(fileNames === undefined){
                    console.log("No file selected");
                }else{
                    document.getElementById("actual-file").value = fileNames[0];
    
    
                    readFile(fileNames[0], function(myData){
                     console.log(myData);
                    });
    
                }
            }); 
        },false);
    
        function readFile(filepath, callBack) {
            fs.readFile(filepath, 'utf-8', function (err, data) {
                if(err){
                    alert("An error ocurred reading the file :" + err.message);
                    return;
                }
                callBack(data);
                //document.getElementById("content-editor").value = data;
            });
        }
    

    【讨论】:

    • 这不行,fs.readFile是异步函数,readFile函数会立即返回,没有值。
    猜你喜欢
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多