What I'm trying to do:
What's happening:
Questions
(Firebase reference has been changed to 'fbname' in code below).
Any help would be greatly appreciated.
function Comments(fbname) { var firebase = new Firebase("https://" + fbname + ".firebaseio.com/"); this.firebase = firebase; var commentsRef = firebase.child('comments'); this.submitComment = function(text) { commentsRef.push({ text: text }); }; this.onCommentsChanged = function() {}; commentsRef.on('value', function(snapshot) { var comments = snapshot.val(); var preparedComments = []; for (var text in comments) { if (comments.hasOwnProperty(text)) { preparedComments.push({ text: comments.text }) } } this.onCommentsChanged(preparedComments); }.bind(this)); }; $(document).ready(function() { var ll = new Comments('fbname'); $(".comment-form form").submit(function(event) { event.preventDefault(); ll.submitComment($(this).find('input.comment-text').val()); $(this).find("input[type=text]").val("").blur(); return false; }); ll.onCommentsChanged = function(comments) { $(".comments-list").empty(); comments.map(function(comment) { var commentElement = "<li><a href='" + comments.text + "</a></li>"; $(".comments-list").append(commentElement); }); }; });
<html> <head> <script src="https://cdn.firebase.com/js/client/2.2.7/firebase.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <script src="app.js"></script> </head> <body> <h1>Comments Test</h1> <div class="comment-form"> <form> <input type="text" class="comment-text" placeholder="comment text.."/> <input type="submit" value="Comment"/> </form> </div> <div class="comments"> <ul class="comments-list"> </ul> </div> </body> </html>
This code might help:
db.collection("comments").get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
var string = JSON.stringify(doc.data())
var parsed = JSON.parse(string)
document.getElementById("yourcontainer").innerHTML += parsed.text +
'<br>'
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.