skip to Main Content

I have a database where i store all of my users that log in into my website and wanted to make it possible for them to delete their acc. So i made a function that works and can delete users their accs, but i the only problem that i’m having is that i can’t seem to connect the js file that has the eventlistener for the button with my nodejs file with the function.

// node.js file
let id = 1;
/* delete user from mysql db */
async function delUsers() {
    /* actually hardcoded id, need to implement users id when a user can login into the site */
    await pool.query('DELETE FROM user WHERE id = ?', [id]);
    console.log("Deleting..");

};
// js file
'use strict';
console.log('linked');
let del = document.getElementById('delbutton').addEventListener('click', Del);
let a = require('./delete.js');
function Del() {
let b = a.DelUser();
}

I tried using module.exports with module.import, require, sessionstorage, localstorage. I expected my button to work and remove 1 of my users

2

Answers


  1. nodejs is for backend.

    The global object ‘window, document…’ just contain in the browser.

    Login or Signup to reply.
  2. I think that you are missing somme key concept about web infrastructure.

    Your node.js file is a server file executed by node.js and your "js" file is a front end file executed by your browser, in order to communicate they need to use a protocol, the main protocol used on the web is http, so you need to setup a web server with your node.js file and then call a route from your front end js file with an http request.

    I would suggest you to read some article about front-end and back-end concept and how they interact.

    Here is an article but there is a lot of documentation about the subject

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search