Hi, i'm a new javascript programmer. I'm coding a simple Web site but I have a problem with getelementbyid. I have connected the site to firebase and I'm Tring to modify some element after login. When the program in a file.js change page from access.html to profile.html the console shoe me an error about the element in access. Getelementbyid return null when I am in profile.html and equally in access.html I don't know how to solve the problem...please help
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (4)
can you share the code?
import { initializeApp } from "gstatic.com/firebasejs/9.4.0/fireb...";
import { getDatabase, set, ref, update } from "gstatic.com/firebasejs/9.4.0/fireb...";
import { getAuth, signOut, createUserWithEmailAndPassword, signInWithEmailAndPassword,onAuthStateChanged } from "gstatic.com/firebasejs/9.4.0/fireb...";
import * as func2 from "./func2.js";
const firebaseConfig = {
apiKey: "AIzaSyA0PdI6RRM_VqyxEsUYuPe0Gu_TUrbbuuQ",
authDomain: "beeteam-2a5f7.firebaseapp.com",
databaseURL: "beeteam-2a5f7-default-rtdb.firebas...",
projectId: "beeteam-2a5f7",
storageBucket: "beeteam-2a5f7.appspot.com",
messagingSenderId: "223479016271",
appId: "1:223479016271:web:383e9319470cfaf4bd733a",
measurementId: "G-04ERX78GQJ"
};
// Initialize Firebase
export const app= initializeApp(firebaseConfig);
const database = getDatabase(app);
const auth=getAuth();
let sub=document.getElementById("sub");
let log=document.getElementById("login");
let out=document.getElementById("logout");
sub.addEventListener('click', (e) =>{
var nome=document.getElementById("nreg").value;
var cognome=document.getElementById("creg").value;
var numero=document.getElementById("numreg").value;
var email=document.getElementById("ereg").value;
var password=document.getElementById("preg").value;
createUserWithEmailAndPassword(auth, email, password,nome,cognome,numero)
.then((userCredential) => {
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alert(errorMessage);
});
});
log.addEventListener('click', (e) => {
var email=document.getElementById("emailacc").value;
var password=document.getElementById("pacc").value;
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
const user = userCredential.user;
const dt= new Date();
update(ref(database,'users/' + user.uid),{
last_login : dt,
})
alert("User Loged in!");
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
alert(errorMessage);
});
});
the console show me this error only after the change of the page
Photo error:
dev-to-uploads.s3.amazonaws.com/up...
you can add null check
if ( sub != null) {
//code
}