Chatbox for website

Joined
Jun 14, 2018
Messages
110
Reaction score
1
i'm trying to create a chatbox for our website but
when i try to register i get errors, YES I'VE BEEN TRYING HARD TO FIGURE THEM OUT

error:
Uncaught (in promise) TypeError: Cannot read properties of null (reading 'value')
at registerBtn.onclick (script.js:1:780)

the js script i do have my api keys and everything else that goes with it,
i'm using firebase authentication as well.

JavaScript:
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
const db = firebase.firestore();

const loginBtn = document.getElementById('loginBtn');
const registerBtn = document.getElementById('registerBtn');
const sendMessage = document.getElementById('sendMessage');
const logoutBtn = document.getElementById('logoutBtn');
const chatContainer = document.getElementById('chat-container');
const authContainer = document.getElementById('auth-container');

// User login function
loginBtn.onclick = async () => {
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
    try {
        await auth.signInWithEmailAndPassword(username, password);
        authContainer.style.display = 'none';
        chatContainer.style.display = 'block';
        loadMessages();
    } catch (error) {
        alert(error.message);
    }
};

// User registration function
registerBtn.onclick = async () => {
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
    try {
        await auth.createUserWithEmailAndPassword(username, password); // Use username in place of email
        alert('User registered successfully!');
    } catch (error) {
        alert(error.message);
    }
};

// Send message function
sendMessage.onclick = async () => {
    const msg = document.getElementById('messageInput').value;
    const user = auth.currentUser;
    if (user) {
        await db.collection('messages').add({
            username: user.email,
            message: msg,
            timestamp: firebase.firestore.FieldValue.serverTimestamp()
        });
        document.getElementById('messageInput').value = '';
    }
};

// Logout function
logoutBtn.onclick = () => {
    auth.signOut();
    authContainer.style.display = 'block';
    chatContainer.style.display = 'none';
};

// Load messages function
const loadMessages = () => {
    db.collection('messages').orderBy('timestamp')
        .onSnapshot(snapshot => {
            const messagesDiv = document.getElementById('messages');
            messagesDiv.innerHTML = '';
            snapshot.forEach(doc => {
                const data = doc.data();
                messagesDiv.innerHTML += `<p><strong>${data.username}:</strong> ${data.message}</p>`;
            });
        });
};

// Check authentication state
auth.onAuthStateChanged(user => {
    if (user) {
        authContainer.style.display = 'none';
        chatContainer.style.display = 'block';
        loadMessages();
    } else {
        authContainer.style.display = 'block';
        chatContainer.style.display = 'none';
    }
});
 
Joined
Jul 4, 2023
Messages
503
Reaction score
63
The error probably occurs because document.getElementById('username') or document.getElementById('password')
returns null, which means that elements with ids 'username' or 'password' do not exist in the HTML at the time the script is executed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,039
Messages
2,570,375
Members
47,026
Latest member
TishaMadri

Latest Threads

Top