我正在制作评论系统。我的问题是,当我发表评论并重新加载页面时,评论已不再是。我在做什么错?

发布于 2025-02-12 12:30:12 字数 2810 浏览 0 评论 0原文

我正在制作评论系统。我的问题是我发表评论时 重新加载页面评论已不再。我在做什么错?

/ Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const db = getDatabase();

// getting user input from form to store in firebase
const name = document.querySelector('.form--1');
const head = document.querySelector('.form--2');
const message = document.querySelector('.form--3');

获取数据的地方

const postNewComment = function () {

    set(ref(db, 'comments/'), {
        Name: name.value,
        Header: head.value,
        Message: message.value
    }).then(() => {
        console.log('data stored successfully');
    }).catch((error) => {
        console.log('unsuccessful, error ' + error);
    });
};

这是我从数据库的注释表格中 离开。

const postBtn = document.querySelector('.btn--post');

// creates html elements
function elementFromHtml(html) {
    const templat = document.createElement('template');
    templat.innerHTML = html.trim();
    return templat.content.firstElementChild;
}

const postComment = function (e) {
    const sectionComments = document.querySelector('.scroll');
    e.preventDefault();
    postNewComment();

    // reads data from firebase database and post comment to dom
    const dbref = ref(db);

    get(child(dbref, 'comments/')).then((snapshot) => {
        if (snapshot.exists()) {
            const commentData = snapshot.val();
            console.log(commentData);

            const html = `
                <div class="row">
                    <div class="comments">
                    <figure class="comments__shape bg-color">
                        <img
                        src=""
                        alt=""
                        class="comments__image"
                        />
                        <figcaption class="comments__caption">${commentData.Name}</figcaption>
                    </figure>
                    <div class="comments__text">
                        <h3 class="heading-tertiary u-margin-bottom-small f-title">
                        ${commentData.Header}
                        </h3>
                        <p class="comments__story">
                        ${commentData.Message}
                        </p>
                    </div>
                    </div>
                </div>
                `;


            sectionComments.appendChild(elementFromHtml(html));
        } else {
            console.log('no data found');
        }
    }).catch((error) => {
        console.log('unsuccessful, error ' + error);
    });

    // reset feilds
    document.querySelector('.form').reset();

};
postBtn.addEventListener('click', postComment);

I am making a comment system. My problem is when I post a comment and
reload the page the comment is no more. What am I doing wrong?

/ Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
const db = getDatabase();

// getting user input from form to store in firebase
const name = document.querySelector('.form--1');
const head = document.querySelector('.form--2');
const message = document.querySelector('.form--3');

Here is where I get the data from the comment form for database

const postNewComment = function () {

    set(ref(db, 'comments/'), {
        Name: name.value,
        Header: head.value,
        Message: message.value
    }).then(() => {
        console.log('data stored successfully');
    }).catch((error) => {
        console.log('unsuccessful, error ' + error);
    });
};

Everything else below is a function to create an html element and a function to receive data from database and post to dom but as I said it works but once I reload page the comment goes away.

const postBtn = document.querySelector('.btn--post');

// creates html elements
function elementFromHtml(html) {
    const templat = document.createElement('template');
    templat.innerHTML = html.trim();
    return templat.content.firstElementChild;
}

const postComment = function (e) {
    const sectionComments = document.querySelector('.scroll');
    e.preventDefault();
    postNewComment();

    // reads data from firebase database and post comment to dom
    const dbref = ref(db);

    get(child(dbref, 'comments/')).then((snapshot) => {
        if (snapshot.exists()) {
            const commentData = snapshot.val();
            console.log(commentData);

            const html = `
                <div class="row">
                    <div class="comments">
                    <figure class="comments__shape bg-color">
                        <img
                        src=""
                        alt=""
                        class="comments__image"
                        />
                        <figcaption class="comments__caption">${commentData.Name}</figcaption>
                    </figure>
                    <div class="comments__text">
                        <h3 class="heading-tertiary u-margin-bottom-small f-title">
                        ${commentData.Header}
                        </h3>
                        <p class="comments__story">
                        ${commentData.Message}
                        </p>
                    </div>
                    </div>
                </div>
                `;


            sectionComments.appendChild(elementFromHtml(html));
        } else {
            console.log('no data found');
        }
    }).catch((error) => {
        console.log('unsuccessful, error ' + error);
    });

    // reset feilds
    document.querySelector('.form').reset();

};
postBtn.addEventListener('click', postComment);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文