Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'style' of null - Google Sign-In Button

I'm trying to implement Google sign in for my website. The Sign-In button shows up correctly and signs-people in well initially. My problem occurs when I log out after having used the website and try to move to the Sign-In page (I'm using React, so it's all one page). I use the exact same function to render the Sign-In page but it gives me a "cb=gapi.loaded_0:249 Uncaught TypeError: Cannot read property 'style' of null". The error in gapi occurs here (at least I think):

 a.El;window.document.getElementById((c?"not_signed_in":"connected"

This is how I initially add the Sign-In button to be rendered:

elements.push(h('div.g-signin2',{'data-onsuccess': 'onSignIn'}))
        return h('div.page_content',elements)

which I later render with a ReactDOM.render call.

Here's how I handle SignOut and SignIn:

function signOut() {
    var auth2 = gapi.auth2.getAuthInstance();
    auth2.signOut().then(function () {
      // console.log('User signed out.');
      signedin = false;
      auth2 = null;
      renderPage()
    });
  }

var google_idtoken;
var signedin = false;

// set auth2 to null to indicate that google api hasn't been loaded yet
var auth2 = null;

function onSignIn(googleUser) {
    auth2 = gapi.auth2.getAuthInstance({
        client_id: 'ClientID.apps.googleusercontent.com'
    });
    google_idtoken = googleUser.getAuthResponse().id_token;
    wrongemail = true;
  // if(auth2 != null && auth2.isSignedIn.get() == true){

    if ((((auth2.currentUser.get()).getBasicProfile()).getEmail()).split("@").pop() == 'domain.com'){
        signedin = true
        wrongemail = false
    }
    updateSources()
    // renderPage()
}
like image 827
Filip Bujaroski Avatar asked May 26 '16 23:05

Filip Bujaroski


People also ask

How to fix cannot read property style of null in JavaScript?

To resolve the "Cannot read property 'style' of null" error, make sure that the DOM element you're accessing the style property on exists. In the example, an element with the provided id does not exist in the DOM, so the getElementById method returns null .

Can not reading properties of null?

The "Cannot read property 'click' of null" error occurs when trying to call the click method on a null value. To solve the error, run the JS script after the DOM elements are available and make sure you only call the method on valid DOM elements.

What does Cannot read property ID of null mean?

If the error message says "Could not read property 'id' of null" it means that the guild property exists, but it is null.


1 Answers

I had a similar problem though I'm not sure it's exactly related to your issue. I had added the button in the html as:

<div id="gdbtn" class="g-signin2" data-onsuccess="onSignIn"></div>

But I wanted to customize the rendering of the button according to Google's instructions so I added the platform script as:

<script src="https://apis.google.com/js/platform.js?onload=renderGDriveButton"></script>

With the following onload function:

function renderGDriveButton() {
  gapi.signin2.render('gdbtn', { 
    'height': 50
  });
}

I got the same error you did: "Uncaught TypeError: Cannot read property 'style' of null". My problem was that I included the attributes: class="g-signin2" data-onsuccess="onSignIn" Once I took those attributes out and added the button as:

<div id="gdbtn"></div>

Then the error went away. Seems very similar to your issue. You might try adding the button through an onload function. Just a thought. You can check out the docs at: https://developers.google.com/identity/sign-in/web/build-button

like image 179
VirtualMitra Avatar answered Sep 30 '22 18:09

VirtualMitra