locked
Getting error when trying to write into a file using java script in my Metro App

    Question

  •  

    I am trying to find out an issue in my Metro App and thought I will create a file to copy all the log messages in ther.. I tried the following

     

    varLogFile = "LogFile.txt";

    Windows.Storage.DownloadsFolder.createFileAsync("LogFile.txt").done(

    function(file) {

    LogFile = file;

    });

    Windows.Storage.FileIO.appendLinesAsync(LogFile, "Test").done();   <-- line 26

     

    but then I get the following error:

    Unhandled exception at line 26, column 5 in ms-appx://windowsmultimediaperformanceapplication/js/default.js

    0x800a000d - JavaScript runtime error: Type mismatch

     

    It creates the file properly but it fails to write in it. Any help will be appreciated. I looked at the sample code provided and referred to that when writing this.

     

    Wednesday, June 5, 2013 5:50 PM

Answers

  • It looks like you're trying to write synchronous code around async functions. If you put the appendLinesAsync call inside the anonymous completed handler you pass to the first done, then it should work. (If you've been working in C#, note that then/done is not the same as the await keyword, so you can't write code with that kind of structure.)

    LogFile, when you first create it, is a string. Your first call to createFileAsync().done() will return right away, before the completed handler is called. You're then calling appendLinesAsync next, meaning that LogFile is still a string, hence the type mismatch. Later, when createFileAsync finishes, you'll enter the completed handler and set LogFile to a StorageFile object. In this case you should really just eliminate the variable altogether and write the code like this:

    Windows.Storage.DownloadsFolder.createFileAsync("LogFile.txt").done(
    function(file) {
        //You don't need .done here unless you're attaching handlers
        Windows.Storage.FileIO.appendLinesAsync(file, "Test");
    });
    

    Kraig, author of Programming Windows 8 Apps in HTML, CSS, and JavaScript (free ebook)

    • Marked as answer by Kajal8 Thursday, June 6, 2013 12:20 AM
    Wednesday, June 5, 2013 6:22 PM

All replies

  • It looks like you're trying to write synchronous code around async functions. If you put the appendLinesAsync call inside the anonymous completed handler you pass to the first done, then it should work. (If you've been working in C#, note that then/done is not the same as the await keyword, so you can't write code with that kind of structure.)

    LogFile, when you first create it, is a string. Your first call to createFileAsync().done() will return right away, before the completed handler is called. You're then calling appendLinesAsync next, meaning that LogFile is still a string, hence the type mismatch. Later, when createFileAsync finishes, you'll enter the completed handler and set LogFile to a StorageFile object. In this case you should really just eliminate the variable altogether and write the code like this:

    Windows.Storage.DownloadsFolder.createFileAsync("LogFile.txt").done(
    function(file) {
        //You don't need .done here unless you're attaching handlers
        Windows.Storage.FileIO.appendLinesAsync(file, "Test");
    });
    

    Kraig, author of Programming Windows 8 Apps in HTML, CSS, and JavaScript (free ebook)

    • Marked as answer by Kajal8 Thursday, June 6, 2013 12:20 AM
    Wednesday, June 5, 2013 6:22 PM
  • Thanks for the help Kraig.. It works now.

    Thanks

    Kajal

    Thursday, June 6, 2013 12:20 AM