@firebase/database: FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot read property...












0















UPDATED CODE



So this is making me crazy...I am creating a blog app (Vanilla Js + Firebase).




  • onDeleteButton deletes a single blog post. It deletes both front end
    and backend. no error.


  • onEditButton edits a single blog post. It works fine as well.


  • When I first edit a post and after that delete the same post, it deletes from both back and and front end, seemingly works fine BUT in console log throws me and error:
    Uncaught TypeError: Cannot read property 'title' of null.
    The error message points at the storeTitle.value = editPost.title; line in onEditButton function.



Why does onEditButton function gets called after deleting a post, but only once it has been edited once?




function onDeleteButton(event) {
event.preventDefault();
let id = event.target.parentElement.getAttribute('id');
let deletePost = document.getElementById(id);
firebase.database().ref('posts/' + id).remove();
deletePost.parentNode.removeChild(deletePost);
}

function onEditButton(event) {
event.preventDefault();
let editButton = document.getElementById('edit');
editButton.removeAttribute("hidden");
let id = event.target.parentElement.getAttribute('id');
let posts = firebase.database().ref('posts/' + id);
let storeTitle = document.getElementById('blog-title');
let storeContent = document.getElementById('blog-content');
localStorage.setItem("postID", id);
document.getElementById("blog-id-storage").innerHTML = localStorage.getItem("postID");
posts.on('value', function(snapshot) {
let editPost = snapshot.val();
storeTitle.value = editPost.title;
storeContent.value = editPost.content;
});
}


function enterEditingMode() {
if (document.getElementById('editing-mode-button').innerHTML === "Exit editing mode") {
exitEditingMode();
}
else {

...
addDeleteButton.addEventListener("click", this.onDeleteButton);
addEditButton.addEventListener("click", this.onEditButton);

})
}

}


html:



<button id="editing-mode-button" onclick="enterEditingMode()" class="btn btn-outline-primary">Editing mode</button>









share|improve this question

























  • It will be helpful to see how the binding of onEditButton and onDeleteButton to their buttons in the code

    – Moti Azu
    Nov 24 '18 at 22:11











  • I have edited the code above.

    – Gabriella Csernus
    Nov 26 '18 at 12:46











  • As many times I click on the edit button, when deleting the same post I get the error message the same amount of times.

    – Gabriella Csernus
    Nov 29 '18 at 18:44
















0















UPDATED CODE



So this is making me crazy...I am creating a blog app (Vanilla Js + Firebase).




  • onDeleteButton deletes a single blog post. It deletes both front end
    and backend. no error.


  • onEditButton edits a single blog post. It works fine as well.


  • When I first edit a post and after that delete the same post, it deletes from both back and and front end, seemingly works fine BUT in console log throws me and error:
    Uncaught TypeError: Cannot read property 'title' of null.
    The error message points at the storeTitle.value = editPost.title; line in onEditButton function.



Why does onEditButton function gets called after deleting a post, but only once it has been edited once?




function onDeleteButton(event) {
event.preventDefault();
let id = event.target.parentElement.getAttribute('id');
let deletePost = document.getElementById(id);
firebase.database().ref('posts/' + id).remove();
deletePost.parentNode.removeChild(deletePost);
}

function onEditButton(event) {
event.preventDefault();
let editButton = document.getElementById('edit');
editButton.removeAttribute("hidden");
let id = event.target.parentElement.getAttribute('id');
let posts = firebase.database().ref('posts/' + id);
let storeTitle = document.getElementById('blog-title');
let storeContent = document.getElementById('blog-content');
localStorage.setItem("postID", id);
document.getElementById("blog-id-storage").innerHTML = localStorage.getItem("postID");
posts.on('value', function(snapshot) {
let editPost = snapshot.val();
storeTitle.value = editPost.title;
storeContent.value = editPost.content;
});
}


function enterEditingMode() {
if (document.getElementById('editing-mode-button').innerHTML === "Exit editing mode") {
exitEditingMode();
}
else {

...
addDeleteButton.addEventListener("click", this.onDeleteButton);
addEditButton.addEventListener("click", this.onEditButton);

})
}

}


html:



<button id="editing-mode-button" onclick="enterEditingMode()" class="btn btn-outline-primary">Editing mode</button>









share|improve this question

























  • It will be helpful to see how the binding of onEditButton and onDeleteButton to their buttons in the code

    – Moti Azu
    Nov 24 '18 at 22:11











  • I have edited the code above.

    – Gabriella Csernus
    Nov 26 '18 at 12:46











  • As many times I click on the edit button, when deleting the same post I get the error message the same amount of times.

    – Gabriella Csernus
    Nov 29 '18 at 18:44














0












0








0








UPDATED CODE



So this is making me crazy...I am creating a blog app (Vanilla Js + Firebase).




  • onDeleteButton deletes a single blog post. It deletes both front end
    and backend. no error.


  • onEditButton edits a single blog post. It works fine as well.


  • When I first edit a post and after that delete the same post, it deletes from both back and and front end, seemingly works fine BUT in console log throws me and error:
    Uncaught TypeError: Cannot read property 'title' of null.
    The error message points at the storeTitle.value = editPost.title; line in onEditButton function.



Why does onEditButton function gets called after deleting a post, but only once it has been edited once?




function onDeleteButton(event) {
event.preventDefault();
let id = event.target.parentElement.getAttribute('id');
let deletePost = document.getElementById(id);
firebase.database().ref('posts/' + id).remove();
deletePost.parentNode.removeChild(deletePost);
}

function onEditButton(event) {
event.preventDefault();
let editButton = document.getElementById('edit');
editButton.removeAttribute("hidden");
let id = event.target.parentElement.getAttribute('id');
let posts = firebase.database().ref('posts/' + id);
let storeTitle = document.getElementById('blog-title');
let storeContent = document.getElementById('blog-content');
localStorage.setItem("postID", id);
document.getElementById("blog-id-storage").innerHTML = localStorage.getItem("postID");
posts.on('value', function(snapshot) {
let editPost = snapshot.val();
storeTitle.value = editPost.title;
storeContent.value = editPost.content;
});
}


function enterEditingMode() {
if (document.getElementById('editing-mode-button').innerHTML === "Exit editing mode") {
exitEditingMode();
}
else {

...
addDeleteButton.addEventListener("click", this.onDeleteButton);
addEditButton.addEventListener("click", this.onEditButton);

})
}

}


html:



<button id="editing-mode-button" onclick="enterEditingMode()" class="btn btn-outline-primary">Editing mode</button>









share|improve this question
















UPDATED CODE



So this is making me crazy...I am creating a blog app (Vanilla Js + Firebase).




  • onDeleteButton deletes a single blog post. It deletes both front end
    and backend. no error.


  • onEditButton edits a single blog post. It works fine as well.


  • When I first edit a post and after that delete the same post, it deletes from both back and and front end, seemingly works fine BUT in console log throws me and error:
    Uncaught TypeError: Cannot read property 'title' of null.
    The error message points at the storeTitle.value = editPost.title; line in onEditButton function.



Why does onEditButton function gets called after deleting a post, but only once it has been edited once?




function onDeleteButton(event) {
event.preventDefault();
let id = event.target.parentElement.getAttribute('id');
let deletePost = document.getElementById(id);
firebase.database().ref('posts/' + id).remove();
deletePost.parentNode.removeChild(deletePost);
}

function onEditButton(event) {
event.preventDefault();
let editButton = document.getElementById('edit');
editButton.removeAttribute("hidden");
let id = event.target.parentElement.getAttribute('id');
let posts = firebase.database().ref('posts/' + id);
let storeTitle = document.getElementById('blog-title');
let storeContent = document.getElementById('blog-content');
localStorage.setItem("postID", id);
document.getElementById("blog-id-storage").innerHTML = localStorage.getItem("postID");
posts.on('value', function(snapshot) {
let editPost = snapshot.val();
storeTitle.value = editPost.title;
storeContent.value = editPost.content;
});
}


function enterEditingMode() {
if (document.getElementById('editing-mode-button').innerHTML === "Exit editing mode") {
exitEditingMode();
}
else {

...
addDeleteButton.addEventListener("click", this.onDeleteButton);
addEditButton.addEventListener("click", this.onEditButton);

})
}

}


html:



<button id="editing-mode-button" onclick="enterEditingMode()" class="btn btn-outline-primary">Editing mode</button>






javascript firebase






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 16:20







Gabriella Csernus

















asked Nov 24 '18 at 21:35









Gabriella CsernusGabriella Csernus

5210




5210













  • It will be helpful to see how the binding of onEditButton and onDeleteButton to their buttons in the code

    – Moti Azu
    Nov 24 '18 at 22:11











  • I have edited the code above.

    – Gabriella Csernus
    Nov 26 '18 at 12:46











  • As many times I click on the edit button, when deleting the same post I get the error message the same amount of times.

    – Gabriella Csernus
    Nov 29 '18 at 18:44



















  • It will be helpful to see how the binding of onEditButton and onDeleteButton to their buttons in the code

    – Moti Azu
    Nov 24 '18 at 22:11











  • I have edited the code above.

    – Gabriella Csernus
    Nov 26 '18 at 12:46











  • As many times I click on the edit button, when deleting the same post I get the error message the same amount of times.

    – Gabriella Csernus
    Nov 29 '18 at 18:44

















It will be helpful to see how the binding of onEditButton and onDeleteButton to their buttons in the code

– Moti Azu
Nov 24 '18 at 22:11





It will be helpful to see how the binding of onEditButton and onDeleteButton to their buttons in the code

– Moti Azu
Nov 24 '18 at 22:11













I have edited the code above.

– Gabriella Csernus
Nov 26 '18 at 12:46





I have edited the code above.

– Gabriella Csernus
Nov 26 '18 at 12:46













As many times I click on the edit button, when deleting the same post I get the error message the same amount of times.

– Gabriella Csernus
Nov 29 '18 at 18:44





As many times I click on the edit button, when deleting the same post I get the error message the same amount of times.

– Gabriella Csernus
Nov 29 '18 at 18:44












1 Answer
1






active

oldest

votes


















0














Just in case someone has the same issue in the future, here is the solution.
On the editbutton function I used posts.on('value', function(snapshot) {...} so everytime I run this function once it was listening for changes, the code block in {} was running every time...So instead of posts.on, I had to use posts.once('value', function(snapshot) {...}. .once() without listening for changes



From firebase doc:




Listen for value events
To read data at a path and listen for changes, use the on() or once() methods of firebase.database. Reference to observe events.







share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462569%2ffirebase-database-firebase-warning-exception-was-thrown-by-user-callback-typ%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Just in case someone has the same issue in the future, here is the solution.
    On the editbutton function I used posts.on('value', function(snapshot) {...} so everytime I run this function once it was listening for changes, the code block in {} was running every time...So instead of posts.on, I had to use posts.once('value', function(snapshot) {...}. .once() without listening for changes



    From firebase doc:




    Listen for value events
    To read data at a path and listen for changes, use the on() or once() methods of firebase.database. Reference to observe events.







    share|improve this answer




























      0














      Just in case someone has the same issue in the future, here is the solution.
      On the editbutton function I used posts.on('value', function(snapshot) {...} so everytime I run this function once it was listening for changes, the code block in {} was running every time...So instead of posts.on, I had to use posts.once('value', function(snapshot) {...}. .once() without listening for changes



      From firebase doc:




      Listen for value events
      To read data at a path and listen for changes, use the on() or once() methods of firebase.database. Reference to observe events.







      share|improve this answer


























        0












        0








        0







        Just in case someone has the same issue in the future, here is the solution.
        On the editbutton function I used posts.on('value', function(snapshot) {...} so everytime I run this function once it was listening for changes, the code block in {} was running every time...So instead of posts.on, I had to use posts.once('value', function(snapshot) {...}. .once() without listening for changes



        From firebase doc:




        Listen for value events
        To read data at a path and listen for changes, use the on() or once() methods of firebase.database. Reference to observe events.







        share|improve this answer













        Just in case someone has the same issue in the future, here is the solution.
        On the editbutton function I used posts.on('value', function(snapshot) {...} so everytime I run this function once it was listening for changes, the code block in {} was running every time...So instead of posts.on, I had to use posts.once('value', function(snapshot) {...}. .once() without listening for changes



        From firebase doc:




        Listen for value events
        To read data at a path and listen for changes, use the on() or once() methods of firebase.database. Reference to observe events.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 29 '18 at 23:19









        Gabriella CsernusGabriella Csernus

        5210




        5210
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462569%2ffirebase-database-firebase-warning-exception-was-thrown-by-user-callback-typ%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python