簡體   English   中英

我不能在電子中使用fs.readFile函數

[英]I can't use fs.readFile function in electron

我正在使用電子進行應用程序開發。 但是當我使用fs.readFile函數時,電子拋出錯誤。

碼:

fs.readFile('data.json',(err,data)=>{
  console.log(data);
  win.webContents.send('data:load',JSON.parse(data.toString()));
})

輸出:

{}
null

您需要處理err異常,以檢查fs.readFile()是否正確讀取文件的錯誤詳細信息,通常是在文件路徑后找不到文件:

fs.readFile('data.json',(err,data)=>{
    if (err) {
        console.error(err);
    }
    console.log(data);
    win.webContents.send('data:load',JSON.parse(data.toString()));
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM