Skip to content
This repository was archived by the owner on Apr 10, 2024. It is now read-only.
This repository was archived by the owner on Apr 10, 2024. It is now read-only.

Example not working #39

@cmcknight

Description

@cmcknight

I'm using the following HTML page:

<!doctype html>
<html lang="en">
<head>
    <title>Test Page</title>
</head>
<body>
    <div id="1" class="foo">
    <h2>sample text with <code>inline tag</code></h2>
    <pre id="demo" class="foo bar">foo</pre>
    <pre id="output" class="goo">goo</pre>
    <input id="execute" type="button" value="execute"/>
    </div>
</body>
</html>

with the following code:

var html2json = require('html2json').html2json;
var json2html = require('html2json').json2html;
var fs = require ('fs');

// load the HTML from a file
var htmldoc = fs.readFileSync('./index2.html', "utf8");
console.log(htmldoc);

// convert the innerHTML of the body to JSON
var jsondoc = html2json(htmldoc.body.innerHTML);
console.log(jsondoc);

with the following output:

<!doctype html>
<html lang="en">
<head>
    <title>Test Page</title>
    <meta charset="utf-8" >
</head>
<body>
    <div id="1" class="foo">
    <h2>sample text with <code>inline tag</code></h2>
    <pre id="demo" class="foo bar">foo</pre>
    <pre id="output" class="goo">goo</pre>
    <input id="execute" type="button" value="execute"/>
    </div>
</body>
</html>

[stdin]:12
var jsondoc = html2json(htmldoc.body.innerHTML);
                                     ^

TypeError: Cannot read property 'innerHTML' of undefined
    at [stdin]:12:38
    at Script.runInThisContext (vm.js:132:18)
    at Object.runInThisContext (vm.js:315:38)
    at Object.<anonymous> ([stdin]-wrapper:10:26)
    at Module._compile (internal/modules/cjs/loader.js:1236:30)
    at evalScript (internal/process/execution.js:98:25)
    at internal/main/eval_stdin.js:29:5
    at Socket.<anonymous> (internal/process/execution.js:211:5)
    at Socket.emit (events.js:326:22)
    at endReadableNT (_stream_readable.js:1244:12)
[Finished in 0.249s]

What am I missing? ☹️

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions