当前位置: 首页 > 知识库问答 >
问题:

Electron errors控制台中的“path”参数必须为“string”类型

慎兴业
2023-03-14

electron应用程序代码来自《跨平台桌面应用程序》一书,它旨在向浏览器显示用户文件目录,就像在桌面资源管理器应用程序中一样。

    const os = require('os'); 
    const fs = require('fs');
    const path = require('path');
    const async = require('async');
  


    function getHomeFolder (){
    return os.homedir();
    }
    function retrieveFiles (folderPath,cb) {
       fs.readdir(folderPath,cb);
    }


    function inspectAndDescribeFiles (filepath,cb) {
    let result = {
        file:path.basename(filepath),
        path:filepath,type:''
    };
    fs.stat(filepath,(err,stat) =>{
        if(err){
            cb(err);
        }else{
            if(stat.isFile()){
                result.type = 'file';
            }
            if(stat.isDirectory()){
                result.type = 'directory';
            }
            //cb(err,result);
        }  
    });
    }
    function inspectAndDescribeFiles(folderPath,files,cb){
        async.map(files,(file,asyncCb) => {
            let resolvedFilePath = path.resolve(folderPath,file);
            inspectAndDescribeFiles(resolvedFilePath,asyncCb);
        },cb);
    }
    function displayFileIcons (file){
    const mainArea = document.getElementById('main-area');
    //const template = document.querySelector('item-template');
    //let clone = document.importNode(template.content, true);
    const img = document.querySelector('img').src = './assets/foldericon.png';
    const fileImg = document.querySelector('.filename').innerText =file.file;
    //clone.querySelector('img').src =`assets/${file.type}.png`;
    //clone.querySelector('.filename').innerText =file.file;
    mainArea.appendChild(img);
    }
    function displayFiles (err,files){
    if(err){
        return alert("Sorry we couldn't display your files");
    }
    //files.forEach((file)=>{console.log(file); });
    files.forEach(displayFileIcons);
    }
    function main () {
    const folderPath = getHomeFolder();
    retrieveFiles(folderPath,(err,files) => {
       if(err) {
           return alert('Sorry we could not load your home folder');
       }
     //    files.forEach((file) => {
          
     //     console.log(`${folderPath}/${file}`);
     //    });
     inspectAndDescribeFiles(folderPath,files,displayFileIcons);
     });
     }
     main();

打印到开发人员工具控制台时出现的错误为“Uncaught(in promise)TypeError[ERR_INVALID_ARG_TYPE]:“path”参数必须是string类型。未定义接收。我做错了什么?

共有1个答案

匡玉堂
2023-03-14

此行:inspectanddescripebiles(resolvedFilePath,asyncCb) <代码>检查和描述文件接受三个参数,而不是两个。

 类似资料: