为什么我会收到existSync不是函数错误? node.js

时间:2020-08-11 02:34:45

标签: node.js

为什么我得到fs.existSync不是函数错误?这是一个独特而具体的问题。

代码:

(async () => {
 var inputfile = "c:/computer/image.png"
 var timeouttimeinms = 2000;

 function checkExistsWithTimeout(filePath, timeout) {
        return new Promise(function (resolve, reject) {
            
            //filePath = fileindex
            
            //say image waitign for
            console.log(filePath);
            
            var timer = setTimeout(function () {
                watcher.close();//'Waiting for Completed Image download.'
                reject(new Error("Waiting for image to fully download."));
            }, timeout);
            
            ;
            fs.access(filePath, fs.constants.R_OK, function (err) {
                if (!err) {
                    clearTimeout(timer);
                    watcher.close();
                    resolve();
                }
            });
            
            var dir = path.dirname(filePath);
            var basename = path.basename(filePath)
            //check if file still exists before watching for changes
            if(fs.existSync(filePath)){
                var watcher = fs.watch(dir, function (eventType, filename) {
                    if (eventType === 'rename' && filename === basename) {
                        clearTimeout(timer);
                        watcher.close();
                        //resolve('Image Downloaded Sucessfully.');
                        resolve();
                    }
                });
            
            }
            
        });
    }

    checkExistsWithTimeout(inputfile ,timeouttimeinms );
})();

错误:

(节点:7492)UnhandledPromiseRejectionWarning:TypeError:fs.​​existSync 不是功能

1 个答案:

答案 0 :(得分:0)

您键入了一个功能existSync。
该函数称为existsSync

您在大写S之前缺少s