【问题标题】:Return not waiting for if condition返回不等待if条件
【发布时间】:2022-04-16 20:18:18
【问题描述】:

任何人都可以帮助我。我正在用 Electron js 开发一个小项目。作为初学者,我找不到任何解决此问题的方法。我搜索了很多讨论过的类似问题,但我找不到。

我想用 Sqlite 在本地存储数据,但是当应用程序在新系统中首次打开时,用户应该设置数据库位置。为此,我在主进程和渲染器中编写了一些代码。

这是主进程中的代码

ipcMain.handle('read-user-data', async (event, fileName) => {

    const checkPath = path.join(app.getPath('userData'), '\Local Storage/config.json')

    const dbPathExist = fs.readJsonSync(checkPath, { throws: false })

    if (dbPathExist === null) {
        dialog.showOpenDialog({ properties: ['openFile'] })
            .then(dgRresult => {
                if (!dgRresult.canceled) {

                    try {
                        fs.writeJson(checkPath, { dbPath: dgRresult.filePaths[0] })
                        FinaldbPath = dgRresult.filePaths[0]
                        return FinaldbPath

                    } catch (err) {
                        console.error("521: " + err)
                    }

                } else {
                    win.close()
                }
            })
            .catch((error) => {
                console.error("522: " + error)
            })
    } else {

        fs.readJson(checkPath)
            .then(packageObj => {
                FinaldbPath = packageObj.dbPath

                console.log(typeof (FinaldbPath))
                return FinaldbPath
            })
            .catch(err => {
                console.error(err)
            })
    }
})   

在渲染中

dbConnection: function () {
    ipcRenderer.invoke('read-user-data')
        .then(dbPath => {
            console.log('invoke is: ' +  dbPath)
        })
}

但结果总是不确定。 我猜异步功能有问题。但我找不到合适的解决方案

谁能帮帮我

【问题讨论】:

  • 您使用了async 句柄函数,但从未在其中使用await 任何内容? ipcMain.handle 甚至期望返回值吗?
  • 您没有在 ipcMain.handle 监听器中返回任何内容。您应该返回一个承诺或使用await 而不是then。我建议您阅读更多关于 Promise 和 async/await 在 JavaScript 中的工作原理
  • 感谢您的快速重播。问题解决了。。
  • @ShafeequeOT 如果您的问题已解决,请写下答案,发布并接受。这可能会在未来帮助其他人。谢谢!
  • 问题是您将.thenawait 混合使用——而不是仅使用await——然后也没有等待得到的承诺。

标签: javascript node.js asynchronous async-await electron


【解决方案1】:

同步方法解决了这个问题

这里是修改后的代码

ipcMain.handle('read-user-data', async (event, fileName) => {

    const checkPath = path.join(app.getPath('userData'), '\Local Storage/config.json')

    const dbPathExist = fs.readJsonSync(checkPath, { throws: false })

    if (dbPathExist === null) {
        selectedPath =  dialog.showOpenDialogSync({ properties: ['openFile'] })
   
        if (selectedPath) {

            try {
                fs.writeJson(checkPath, { dbPath: selectedPath[0] })
                FinaldbPath = selectedPath[0]
                return FinaldbPath

            } catch (err) {
                console.error("521: " + err)
            }


        } else {
            win.close()
        }
 
    } else {

        const packageObj = fs.readJsonSync(checkPath)
        FinaldbPath = packageObj.dbPath

        return FinaldbPath
    }
})

【讨论】:

  • 这会阻塞线程。最好正确使用await
猜你喜欢
  • 2020-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 2019-07-30
  • 2022-12-11
相关资源
最近更新 更多