我正在制作评论系统。我的问题是,当我发表评论并重新加载页面时,评论已不再是。我在做什么错?
我正在制作评论系统。我的问题是我发表评论时 重新加载页面评论已不再。我在做什么错?
/ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论