jerosoler / Drawflow

Simple flow library 🖥️🖱️
https://jerosoler.github.io/Drawflow/
MIT License
4.65k stars 722 forks source link

Importing data in a string format causes output to be 'undefined' #482

Closed dinukapj closed 2 years ago

dinukapj commented 2 years ago

Love the library. I have an issue with the import, however. My Exported JSON looks like this:

var test = '{ "drawflow": { "Home": { "data": { "1": { "id": 1, "name": "begin", "data": {}, "class": "begin", "html": "\n<div>\n<div class=\"title-box\"><i class=\"fas fa-forward\"></i> Begin</div>\n</div>\n", "typenode": false, "inputs": {}, "outputs": { "output_1": { "connections": [] } }, "pos_x": 50, "pos_y": 50 } } }, "Other": { "data": {} } }}'

editor.import(test);

When I try to run the app, it ends up with this message:

drawflow.min.js:1 Uncaught TypeError: Cannot read properties of undefined (reading 'Home')
    at i.load (drawflow.min.js:1:4152)
    at i.import (drawflow.min.js:1:45397)
    at reader.onload (automation.html:658:38)

But if I remove the quotation marks from the sample JSON, it works:

var test = { "drawflow": { "Home": { "data": { "1": { "id": 1, "name": "begin", "data": {}, "class": "begin", "html": "\n<div>\n<div class=\"title-box\"><i class=\"fas fa-forward\"></i> Begin</div>\n</div>\n", "typenode": false, "inputs": {}, "outputs": { "output_1": { "connections": [] } }, "pos_x": 50, "pos_y": 50 } } }, "Other": { "data": {} } }}

However, since I'm trying to load the JSON from a file on the computer (from a file selector), the output format will most likely be in string format. How can I resolve this?

jerosoler commented 2 years ago

Thanks

Use JSON.parse to convert to object.

https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

var test = JSON.parse(YOUR_STRING_TEXT);
dinukapj commented 2 years ago

Thanks

Use JSON.parse to convert to object.

https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

var test = JSON.parse(YOUR_STRING_TEXT);

Thank you very much! That worked. I'll close this issue.