Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.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/1/angular/33.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 Firebase-在删除项时调用添加的子项_Javascript_Angular_Firebase_Firebase Realtime Database - Fatal编程技术网

Javascript Firebase-在删除项时调用添加的子项

Javascript Firebase-在删除项时调用添加的子项,javascript,angular,firebase,firebase-realtime-database,Javascript,Angular,Firebase,Firebase Realtime Database,我有一个帖子的列表,我正在获取它们。在“child_added”上,问题是我只想在添加项目时获取帖子,而不是在删除项目时获取帖子。然而,似乎是这样。在“child_added”上,即使在删除帖子时也会调用。这不是我需要的 以下是我的功能: if(fromClick) { this.subscription.off(); this.firstRun = true;} this.postFeed = new Array(); this.subscription = this.database.dat

我有一个帖子的列表,我正在获取它们。在“child_added”上,问题是我只想在添加项目时获取帖子,而不是在删除项目时获取帖子。然而,似乎是这样。在“child_added”上,即使在删除帖子时也会调用。这不是我需要的

以下是我的功能:

if(fromClick) { this.subscription.off(); this.firstRun = true;}
this.postFeed = new Array();
this.subscription = this.database.database.ref('/posts/'+this.locationId)
   .orderByChild('created')
   .limitToLast(10);

this.subscription.on('child_added', (snapshot) => {
   this.postFeed.push(snapshot.val());
});
因此,它显示last 10,当添加一个项目时,它也会将该项目添加到数组中,但当删除一个项目时,它会再次被调用

我怎样才能防止这种情况?因此,只有添加的帖子才会调用?

如果您正在调用:

this.database.database.ref('/posts/'+this.locationId)
   .orderByChild('created')
   .limitToLast(10);
您正在创建一个包含最近10篇文章的查询。如果你删除其中一个,另一个将成为第十个最新帖子,因此你会为此添加一个child_

我唯一能想象的是只得到一次10,然后做一次限时1:

this.database.database.ref('/posts/'+this.locationId)
   .orderByChild('created')
   .limitToLast(10)
   .once("value", function(snapshot) {
     snapshot.forEach(function(child) {       
       // These are the initial 10 children
     });
   });
this.database.database.ref('/posts/'+this.locationId)
   .orderByChild('created')
   .limitToLast(1)
   .on("child_added", function(snapshot) {
     // This is the latest child, but this will also fire when you remove the current latest
   });