Search code examples
javascriptnode.jspostgresqlnode-postgres

Function not defined in html


I'm currently learning javascript, and i stumble on this error: myfunction not defined

This is my html:

<!DOCTYPE html>
<hmtl>
<head>
        <script src="app.js"></script>

</head>
<body>

    <input type="button" onclick="myfunction()" value="run external javascript"/>
</body>
</hmtl>

This is my app.js:

//@ts-check
debugger

function hello() {
  alert("Hello World!");
};


import pg from "pg";

function myfunction(){

const connectionString = process.env.DATABASE_URL || connection_string;

const client = new pg.Client(connectionString);
client.connect();
var qry = client.query('SELECT NOW()', (err, res) => {
    console.log(err, res)
    client.end()
   })

 qry.on("row", function (row, result) {
   result.addRow(row);
 });

qry.on('end', () => { client.end(); });

};

Someone can give me some advice on how to solve, and how to prevent the same from happening in the future?


Solution

  • Your JavaScript is targetting Node.js.

    You need to run it on Node.js.

    You can't embed it in an HTML document and run it in a browser.

    (It is possible to write JS — to do some things — so it can run in a browser or in Node.js … this isn't one of those things).