0

I have been trying to restore all my used blocks from localStorage.I am looping through all the local saved blocks but I got the following error. It would be great if I can get some help.

Error:

blockly_compressed.js:1274 Uncaught Error: Blockly.Xml.textToDom expected an 
<xml> document.
at Object.Blockly.Xml.textToDom (blockly_compressed.js:1274)
at restoreWorkspace ((index):312)
at HTMLButtonElement.onclick ((index):115)
Blockly.Xml.textToDom   @   blockly_compressed.js:1274
restoreWorkspace    @   (index):312
onclick @   (index):115

Code to restore all the blocks:

 // restore blocks
    function restoreWorkspace() {
        for (var i = 0; i < localStorage.length; i++){
            let program=(localStorage.getItem(localStorage.key(i)));
            Blockly.mainWorkspace.clear();
            let textToDom = Blockly.Xml.textToDom(program);
            Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, textToDom);
        }
    }

Code to save compositions:

   function saveWorkspace() {
        let xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
        let domToPretty = Blockly.Xml.domToPrettyText(xml);
        var ranInt = Math.floor(Math.random() * 10);
        window.localStorage.setItem("myProgram"+ranInt, domToPretty);
    }
Sakhawat Hossain
  • 454
  • 8
  • 23
  • Hrm, I don't see anything immediately odd about your code... if you log out domToPretty in saveWorkspace + log out the value of program in restoreWorkspace, what values do you get? – Amber B. Nov 07 '19 at 13:59
  • Sorry , can you please eloborate your opinion again? – Sakhawat Hossain Nov 10 '19 at 06:19
  • 2
    Please log out the value of the domToPretty variable and log out the value of the program variable, and update your question with the values. (P.S. you may have a bit more luck getting help in the Blockly Google Group: https://groups.google.com/forum/#!forum/blockly ) – Amber B. Nov 11 '19 at 14:12
  • Thanks for the help. I already tried somethings with google blockly groups. Though no lucks but I am trying to find a way out. – Sakhawat Hossain Nov 11 '19 at 17:56
  • @AmberB. I have tried all the sources and finally i have decided to just keep my code as it is and let other have some thoughts. – Sakhawat Hossain Nov 13 '19 at 07:57
  • console logging the `domToPretty` variable would help in troubleshooting the issue – Nithin Kumar Biliya Jan 06 '20 at 09:35
  • I have already resolved this issue. Though thanks @NithinKumarBiliya – Sakhawat Hossain Jan 06 '20 at 14:30
  • @SakhawatHossain just because you've resolved it doesn't mean it can't help future visitors. Consider writing a [self answer](https://stackoverflow.com/help/self-answer) to provide a resource for future visitors with the same problem as you. – ggorlen Jun 29 '22 at 21:11

0 Answers0