-3

I am trying to make a electron program that could log the text of a TextArea in a HTML file but for some reason I get this error:

(node:8664) UnhandledPromiseRejectionWarning: TypeError: mainWindowHTML.getTextAreaText is not a function at Object.module.exports.Save (C:\Users\user\Documents\JavaScript\node.js\Electron\INTERFACE_EDITOR\menuScripts.js:12:33) at click (C:\Users\user\Documents\JavaScript\node.js\Electron\INTERFACE_EDITOR\index.js:36:37) at MenuItem.click (C:\Users\user\Documents\JavaScript\node.js\Electron\INTERFACE_EDITOR\node_modules\electron\dist\resources\electron.asar\browser\api\menu-item.js:55:9) at Function.executeCommand (C:\Users\user\Documents\JavaScript\node.js\Electron\INTERFACE_EDITOR\node_modules\electron\dist\resources\electron.asar\browser\api\menu.js:30:13) (node:8664) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:8664) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

In my main class I basically load the HTML file into a browser window which works and when a menuitem is clicked it calls this part of my menuScripts.js script:

const url = require('url');
const path = require('path');

module.exports.Save = async(dialog) => {

const mainWindowHTML = url.format({
    pathname: path.join(__dirname , "./htmls/MainWindow.html"),
    protocol: "file",
    slashes: true
    });

const text = mainWindowHTML.getTextAreaText();
    console.log(text);
}

module.exports.New = async(dialog) => {

}

module.exports.Exit = async(window) => {
    window.close();
}

And this is my MainWindow.html file:

<html lang="en">
<head>
    <title>document</title>
</head>
<body>
    <textarea id="code" cols="30" rows="10"></textarea>
</body>
<script>
    module.exports.getTextAreaText = async() => {
        return (document.getElementById("code").innerText)
    }
</script>
</html>
Tal Moshel
  • 160
  • 1
  • 12
  • 2
    `mainWindowHTML` is the result of calling [`url.format`](https://www.npmjs.com/package/url#urlformaturlobj) (`const mainWindowHTML = url.format...`), which returns a **string**. Strings don't have a method called `getTextAreaText`. – T.J. Crowder Dec 17 '18 at 17:55
  • Okay, so how do I reference the MainWindow.html itself? – Tal Moshel Dec 17 '18 at 18:17

2 Answers2

0

Can't you just do:

<html lang="en">
<head>
    <title>document</title>
</head>
<body>
    <textarea id="code" cols="30" rows="10"></textarea>
    <script src="menuScripts.js"></script>
</body>
</html>

and in menuScripts.js:

const url = require('url');
const path = require('path');

module.exports.Save = async(dialog) => {

const mainWindowHTML = url.format({
    pathname: path.join(__dirname , "./htmls/MainWindow.html"),
    protocol: "file",
    slashes: true
    });

const text = document.getElementById("code").innerText;
    console.log(text);
}

module.exports.New = async(dialog) => {

}

module.exports.Exit = async(window) => {
    window.close();
}
Jesper
  • 1,819
  • 3
  • 12
  • 40
0

Okay, well I solved the problem using ipcRenderer instead of exporting functions which didn't really work. I really appreciate the people who tried helping!

Tal Moshel
  • 160
  • 1
  • 12