Cordova 检查现有文件

Cordova checking for existing file

使用 Cordova 5.4.1 和 cordova-plugin-file 4.1.1

我想检查日志文件是否存在,如果不存在,则创建一个。问题是 - 每次我的应用程序启动时,它都会创建新的日志文件。不知何故,它无法查看它是否已经存在。我确实在所需的文件夹中看到了它。

我把代码放在下面了。请帮忙。代码中的问题区域"Issue - even though file exists, this success handler never gets called"。谢谢

var jsGStrLogFName = "MHALog.txt";

document.addEventListener('deviceready', onDeviceReady.bind(this), false);

//deviceready callback
function onDeviceReady() {
    //alert("Cordova deviceready called");
    console.log("Cordova deviceready called");

    //Handle the Cordova pause and resume events
    document.addEventListener('pause', onPause.bind(this), false);
    document.addEventListener('resume', onResume.bind(this), false);

    //05062016 - commented / written in favor of event based online offline check
    //Handle online/offline events
    document.addEventListener('online', onOnline.bind(this), false);
    document.addEventListener('offline', onOffline.bind(this), false);

    //backButton
    document.addEventListener("backbutton", onBackKeyDown.bind(this), false);
    //orientationChange
    window.addEventListener("orientationchange", function () {
        //resize or align
    });

    //TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.

    //logging code - initialize
    //does the file exist or not?
    //Issue - even though file exists, this success handler never gets called
    window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory + jsGStrLogFName,
        function (lgFile) {
            console.log("Got the " + jsGStrLogFName + " file - it exists");
            jsGObjlogOb = lgFile;
            jsFnWriteMHALog("MHA - 1 deviceready fired and device events attached");
        },
        function () {
            console.log(jsGStrLogFName + " doesn't exist... need to create it");

            window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function (dir) {
                dir.getFile(jsGStrLogFName, { create: true }, function (lgFile) {
                    console.log(jsGStrLogFName + " was created.");
                    jsGObjlogOb = lgFile;
                    jsFnWriteMHALog("MHA - 2 deviceready fired and device events attached");
                }, function () {
                    console.log("Can't create file:" + jsGStrLogFName);
                });
            });

        });
}

我发现在使用 visual studio 2015 和 apache cordova 工具调试代码时会发生这种情况。当我部署它时,日志文件被发现并附加到就好了。