【问题标题】:Electron dialog object not return anything, says Promise { <pending> }Promise { <pending> } 说,电子对话对象不返回任何内容
【发布时间】:2020-04-03 22:36:38
【问题描述】:

我正在尝试在我正在构建的本教程应用程序上实现 I/O 操作。其他一切工作正常,我遇到的唯一问题是应用程序没有 console.log() 所需的结果,这应该是对话框中所选文件的位置。

这是截图

【问题讨论】:

  • 你必须在这里分享你的代码兄弟。截图对我们没有帮助。此外,您正在做的可能是异步操作,因此您需要等待 promise 解决,然后再访问所选文件。可能您的代码应该类似于dialog.showOpenDialog(...).then(files=&gt;{ // do your work here })
  • 伙计们,我在读取文件内容时遇到了问题,我已经尝试了所有方法,但我成功兑现了承诺,应用程序正在运行,但文件的内容不能取回。如何确保 dialog.showOpenDialog 返回可以作为字符串缓冲区访问的数据?
  • 您需要使用 FileReader。具体来说,FileReader.readAsArrayBuffer()。你可以在这里阅读更多,只是谷歌在网络中读取文件输入的例子和它的相同之处。 developer.mozilla.org/en-US/docs/Web/API/FileReader

标签: javascript html css electron desktop


【解决方案1】:

正如 r3wt 所说,当您看到 promise&lt;pending&gt; 时,这意味着您没有正确等待异步函数的返回。尝试将您的 getFileFromUser 函数更改为异步,如下所示:

const getFileFromUser = async () => {

    // Triggers the OS' Open File Dialog box. We also pass it as a Javascript
    // object of different configuration arguments to the function

    //This operation is asynchronous and needs to be awaited
    const files = await dialog.showOpenDialog(mainWindow, {
        // The Configuration object sets different properties on the Open File Dialog 
        properties: ['openFile']
    });

    // If we don't have any files, return early from the function
    if (!files) {
        return;
    }

    // Pulls the first file out of the array

    //const file = files[0];
    // Reads from the file and converts the resulting buffer to a string
    //const content = fs.readFileSync(file).toString();

    // Log the Files to the Console
    console.log(files)
}

【讨论】:

    猜你喜欢
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 2020-12-05
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多