T. Andromedon
T. Andromedon

Reputation: 51

Electron crashes when making an HTTPS request inside an addeventlistener callback function

I am making a basic Electron app that logs a user into a system. The username and password are provided from the user via form text inputs, and when the "login" button is clicked, the program makes a couple of https requests, signing the user in.

I have an event listener attached to the "login" button that fires when the button is clicked. Its callback function should get the entered username and password and make the https requests necessary to sign the user in. However, when the https requests are inside of the callback function, the renderer process in Electron crashes (a white screen and "dev tools disconnected" are displayed).

Here is some pseudo-code describing the problem:

//This case crashes Electron
button.addEventListener('click', function() {
    username = document.getElementById('username').value;
    password = document.getElementById('password').value;

    req = https.request(options, function(res) {
        //Login stuff here, including a post request
        //that sends the username and password to a server
    });
    req.end();
});  

When I move the https requests out of the callback function and hardcode the username and password into the program, everything works as expected.

Pseudo-code for the working case:

//This case does not crash Electron
username = "someUsername"
password = "somePassword"

req = https.request(options, function(res) {
    //Login stuff here, including another post request
    //that sends to username and password to a server
});

I want to be able to get the username and password dynamically and make the necessary https requests to sign the user into the system. I've been searching around for any information about this for awhile now, but I haven't been able to find anyone that is having a similar problem.

Any help would be appreciated. Thanks!

Upvotes: 2

Views: 528

Answers (1)

T. Andromedon
T. Andromedon

Reputation: 51

RESOLVED

My button was labeled as "type='submit'" and switching it to "type='button'" solved the problem.

Upvotes: 1

Related Questions