File Pointer from CreateFileAsync in Windows 8 Javascript is undefined-Collection of common programming errors
Writing a Windows 8 app in Javascript using Visual Studio Express. I’m not sure if I am missing a chaining dependency or something else, but I am giving my game users the ability to save their progress into a file (at least that’s the idea). However when I run the following code it appears that even though I can see the file out there, the file pointer is turning out to be undefined. See anything I’m missing?
var myStats = new Array();
myStats[0] = myTitle;
myStats[1] = currPage;
myStats[2] = currStep;
myStats[3] = currTune;
myStats[4] = musicon;
myStats[5] = gameComplete;
var save01File = null;
Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("Asave01.dat", Windows.Storage.CreationCollisionOption.replaceExisting).done(
function (file) {
save01File = file;
},
function (error) {
WinJS.log && WinJS.log(error, "sample", "error");
});
Windows.Storage.FileIO.writeLinesAsync(save01File, myStats).done(
function () {
},
function (error) {
WinJS.log && WinJS.log(error, "sample", "error");
errCount++;
});
if (errCount == 0) {
fileMenuTitle.text = "File Saved - Click Cancel";
fileMenuTitle.text = fileMenuTitle.text + "\nto Return or Select Another File to Load";
fileMenuTitle.color = "green";
}
else {
fileMenuTitle.text = "There was an error saving that file.";
fileMenuTitle.text = fileMenuTitle.text + "\nPlease Make Another Selection.";
fileMenuTitle.color = "firebrick";
}
-
You need to use the promise pattern here:
var myStats = new Array(); myStats[0] = myTitle; myStats[1] = currPage; myStats[2] = currStep; myStats[3] = currTune; myStats[4] = musicon; myStats[5] = gameComplete; var save01File = null; Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("Asave01.dat", Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) { save01File = file; return Windows.Storage.FileIO.writeLinesAsync(file, myStats); }).done(function() { fileMenuTitle.text = "File Saved - Click Cancel"; fileMenuTitle.text = fileMenuTitle.text + "\nto Return or Select Another File to Load"; fileMenuTitle.color = "green"; }, function (error) { WinJS.log && WinJS.log(error, "sample", "error"); fileMenuTitle.text = "There was an error saving that file."; fileMenuTitle.text = fileMenuTitle.text + "\nPlease Make Another Selection."; fileMenuTitle.color = "firebrick"; });
Originally posted 2013-11-09 23:12:03.