Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 数据返回正确,但赢得';是否无法正确显示返回的数据?_Javascript_Jquery_Firebase Realtime Database - Fatal编程技术网

Javascript 数据返回正确,但赢得';是否无法正确显示返回的数据?

Javascript 数据返回正确,但赢得';是否无法正确显示返回的数据?,javascript,jquery,firebase-realtime-database,Javascript,Jquery,Firebase Realtime Database,因此,我目前正努力在p和textarea元素中查看返回的数据 这是我正在使用的函数,您可以看到,我正在接收来自comments节点的每个键,并获得注释标题和消息,这是它应该工作的 function loadComments() { var comments = firebase.database().ref('/comments').once('value', function(snapshot) { snapshot.forEach(functi

因此,我目前正努力在p和textarea元素中查看返回的数据

这是我正在使用的函数,您可以看到,我正在接收来自comments节点的每个键,并获得注释标题和消息,这是它应该工作的

function loadComments() {
    var comments =
        firebase.database().ref('/comments').once('value', function(snapshot) {
            snapshot.forEach(function(data) {
                var childData = data.val();
                var childKey = data.key;
                console.log(childData.title + " " + childData.message);
                Elements.commentTitle.text(childData.title);
                Elements.commentMessage.text(childData.message);

                var clonedNode = $('.listItems:last').clone();
                $(clonedNode).insertAfter('.listItems:last');
                $(clonedNode).css({
                    display: "block"
                });
            });
        });
}
但是,当我尝试查看2个元素中的数据时,它将仅显示来自第一个节点的标题和消息,并且根据发送的注释数量,这将重复

比如说,如果说有5条评论,都有不同的数据,那么标题和消息就不同了。这就是你将看到的

 Title
  Message

 Title
  Message

 Title
  Message

 Title
  Message

 Title
  Message
为了以防万一,这里是我正在使用的html代码

<div class="container listItems text-left w-75 justify-content-center" style="display: none">
        <p class="titleBody"><strong></strong></p>
        <ul><p class="commentBody"></p></ul>
</div>


问题在于您克隆了现有元素。您应该在克隆元素后立即替换数据

下面的代码修复了您投诉的问题

function loadComments() {
    var comments =
        firebase.database().ref('/comments').once('value', function(snapshot) {
            snapshot.forEach(function(data) {
                var childData = data.val();
                var childKey = data.key;

                var clonedNode = $('.listItems:first-child').clone();
                clonedNode.find('.titleBody:first-child > strong:first-child').text(childData.title);
                clonedNode.find('.commentBody:first-child').text(childData.message);
                clonedNode.insertAfter('.listItems:last-child');
                clonedNode.css({
                    display: "block"
                });
            });
        });
}

什么是
元素的无效子元素
?就您的逻辑而言……您克隆了最后一个,但从不修改cloneSo您的console.log语句显示的数据正确,但显示的元素不正确?看起来您只是复制了第一个元素,而没有对其进行任何修改。@charlietfl元素是我的自定义对象,其中包含所有选定的元素,只是为了使代码更清晰,是的,我刚刚注意到了这一点,谢谢。@Eric Svitok是的,这就是问题所在,但现在已经解决了。谢谢