删除后,Firebase将返回包含未定义对象的数组 [英] Firebase returns array with undefined object after remove

查看:121
本文介绍了删除后,Firebase将返回包含未定义对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从Firebase数据库中移除某个元素时遇到了问题。结构如下(只是一个例子):

  {
users:
[

id:0,
name:Toma Broome
},
{
id:1,
name:Boyd Bolens
}
]
}



  firebase.database()。ref(/ (value,snap => {
users = snap.val();
console.log(users); //注意这一行
usersLoop();
});

然后,我在屏幕上渲染元素,然后尝试删除一个:

  firebase.database()。ref(`/ users / $ {targetEl}`).remove(); 

如果检查Firebase控制台,元素实际上会从数据库中删除,但用户数组中包含 undefined 值,在删除了一些元素之后,它返回一个对象而不是一个数组。

这是删除三在数据库的第一个位置的元素。我的意思是我得到完整的用户列表(我正在上传使用JSON文件),我删除第一个元素,所有其他元素,然后在DOM中重新呈现。然后我再次删除第一个元素,最后当我第三次删除第一个元素时,我从firebase中得到一个对象,显然没有任何东西被渲染,因为代码需要一个数组




(5)[undefined×2,Object,Object,Object]
(5)[undefined×1,Object,Object,Object]对象{3:对象,4:对象}

同样,你可以看到数组不断返回一个长度在数据库中有5个元素。



我正在考虑的选项是使用Lodash从返回的集合中删除未定义的元素和/或把它变成一个数组,但这将藐视未来的应用程序的任何可伸缩性选项。

最后什么似乎比任何事情更奇怪的是,所有的突然数据库结构似乎会改变,直观的看起来就像它一样。



这是Firebase数据库的结构

  -users 
| -0
| -id:0
| -name:value
| -otherprops :value
| -1
| -id:1
| -name:value
| -otherprops:value

解决方案

@cartant提供的链接给出了答案。

是的,firebase确实容忍数据中的数组结构,但不建议使用它们,除非特定的数据不会变得太多或根本不变化。在我的情况下(创建某种CRUD应用程序)不建议,只是使用对象结束一切正常。问题是,如果你上传一个带有数组的json文件,firebase会将它转换成一个对象结构,使用整数作为数组中每个元素的关键字。然后,当我开始删除元素,并达到这一点:$ b​​ $ b

  if(!opt_exportFormat&& allIntegerKeys& amp; ;& maxKey< 2 * numKeys){
//转换为数组。
var array = [];
for(var key in obj)
array [key] = obj [key];

返回数组; $()b $ b} else {
if(opt_exportFormat&&!this.getPriority()。isEmpty()){
obj ['。priority'] = this.getPriority()。val );
}
return obj;



$ b $ p
$ b

删除特定数量的元素后,代码传递给声明,最终导致firebase返回一个对象,创建整个问题。

我结束了这种类型的结构:

  {
users:
{
user_00:{
id:0,
name:Toma Broome
},
user_01:{
id:1,
name:Boyd Bolens





$使用lodash循环遍历对象(或者它可以是 中的 )。我可以像使用数组时那样获取数据。最后使用一个对象结构,我没有更多的集合与未定义的元素。



@cartant,非常感谢!我绝对会用你的意见学习一些东西。我希望你可以发表一个答案,所以我可以将其标记为问题的解决方案,并upvote它,你赢得了先生!


I'm having an issue when I remove an element from a Firebase database. The structure is as follows(just a sample):

{
  "users":
  [
    {
      "id": 0,
      "name": "Toma Broome"
    },
    {
      "id": 1,
      "name": "Boyd Bolens"
    }
  ]
}

I'm getting the users list the connection is like this:

firebase.database().ref("/users/").on("value", snap => {
    users = snap.val();
    console.log( users ); // pay attention to this line
    usersLoop();
});

Then I render the elements on screen and I try to remove one:

firebase.database().ref(`/users/${targetEl}`).remove();

The element is actually removed from the database if I check the Firebase console, but the users array includes undefined values and after deleting some elements it returns an object and not an array.

This is the console after deleting three elements that are in the first position of the database. What I mean is that I get the full list of users (which I'm uploading using a JSON file), I delete the first element, all other elements are then re-rendered in the DOM. Then I remove the first element again and finally when I remove the first element for a third time, I get an object from firebase and obviously nothing is rendered because the code is expecting an array

(5) [undefined × 1, Object, Object, Object, Object]
(5) [undefined × 2, Object, Object, Object]
Object {3: Object, 4: Object}

Also as you can see the array keeps returning a length of 5 when in the database there are less elements.

An option that I'm thinking about is to use Lodash to remove the undefined elements from the collection returned and/or turn it into an array, but that will defy any scalability option of the app looking into the future.

Finally what seems more odd than anything is that all of the sudden the database structure seems to change, when visually keeps looking exactly as it was.

This is the structure of the database on firebase

-users
 |-0
  |-id:0
  |-name:value
  |-otherprops:value
 |-1
  |-id:1
  |-name:value
  |-otherprops:value

解决方案

The links provided by @cartant gave the answer.

Yes, firebase does tolerates array structures in it's data but is not recommended to use them unless the specific data is not going to mutate too much or at all. In my case (creating some sort of CRUD app) is not recommendable, just use objects end everything is ok. The thing is that if you upload a json file with an array in it, firebase will transform that into an object structure using integers as the key of each element in the array. Then when I started to remove elements and reached this point:

if (!opt_exportFormat && allIntegerKeys && maxKey < 2 * numKeys) {
  // convert to array.
  var array = [];
  for (var key in obj)
    array[key] = obj[key];

  return array;
} else {
  if (opt_exportFormat && !this.getPriority().isEmpty()) {
    obj['.priority'] = this.getPriority().val();
  }
  return obj;
}

After deleting a specific number of elements the code passed to the else part of the statement, which ultimately result in firebase returning an object creating the whole issue.

I ended up with this type of structure:

{
  "users":
  {
    "user_00": {
      "id": 0,
      "name": "Toma Broome"
    },
    "user_01": {
      "id": 1,
      "name": "Boyd Bolens"
    }
  }
}

And using lodash to loop through the object (or it could be a for in loop) I was able to get the data in the same way I was doing when using an array. Finally using an object structure I didn't got any more collections with undefined elements in it.

@cartant, thanks a lot!!! I definitely learn something with your comments. I hope that you could post an answer so I can mark it as the solution of the problem and upvote it, you earned it sir!!

这篇关于删除后,Firebase将返回包含未定义对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆