I have a profile page that should show a user's information. if users wish to change the Personal information they will have to click on a button called edit Info which will redirect them to a page with a form on.
The Problem Is: I'm attempting to place the form information back onto the main Dom profile.html into the placeholders so that users can see there previous inputs entered.
But when it comes to rendering the firebase data onto the real-time listener for inserting the firebase saved data onto the profile.html, I'm getting an error Cannot read property 'innerHTML' of null.
Possible Issue: as the form information is located on "name.html" trying to target the "profile.html" is causing Cannot read property 'innerHTML' of null.
The code which I have used:
const name = document.querySelector('.switch_name');
// Render Name Data
const renderName = (data, id) => {
const html = `
<div>
<a href="#" data-id="${id}">
<div class="input-field col s12">
<i class="material-icons prefix">account_circle</i>
<input type="text" class="validate" value="${data.name}"/>
<label>Full Name</label>
</div>
</a>
</div> `;
// -----REPLACE HTML----
name.innerHTML += html;
};
// Savng Profile Info To Database
const profileForm = document.querySelector('.profile-form');
const submitBtn = document.querySelector('.submit');
submitBtn.addEventListener('click', (e) => {
e.preventDefault();
const fullName = profileForm['icon_prefix'].value;
db.collection('profile').doc().set({
name: fullName,
}).then(() => {
window.location.href = "/pages/profile.html";
console.log('data saved');
}).catch(() => {
console.log(error);
});
});
// Real-Time Data
db.collection('profile').onSnapshot((snapshot) => {
snapshot.docChanges().forEach(change => {
if(change.type === 'added'){
renderName(change.doc.data(), change.doc.id);
}
if(change.type === 'removed'){
}
});
});
<div class="switch_name">
<a href="/pages/name.html">
<div class="input-field col s12">
<i class="material-icons prefix">account_circle</i>
<input type="text" class="validate"/>
<label>Full Name</label>
</div>
</a>
</div>