如何处理空数组在火力地堡? [英] How to handle empty arrays in Firebase?

查看:153
本文介绍了如何处理空数组在火力地堡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以有多个汽车可以有多个事故。所以我们可以有:

Say person can have multiple cars, and car can have multiple accidents. So we could have:

# Person with no cars
person:
  name: "Misha"
  cars: []

# Person with free-accident car
person:
  name "Arlen"
  cars:
    0:
      name: "Toyota"
      accidents: []

火力地堡存储这些人的:

Firebase stores these people as:

person:
  name: "Misha"

person:
  name "Arlen"
  cars:
    0:
      name: "Toyota"

所以在JavaScript中我要做以下恢复空数组:(CoffeeScript的)

So in JavaScript I have to do following to restore the empty arrays: (CoffeeScript)

if person.cars?
  for car in person.cars
    car.accidents = [] unless car.accidents?
else
  person.cars = []

有没有更好的方式来处理在火力地堡空数组,而无需编写本不必要的JavaScript code?

Is there a better way to handle empty arrays in Firebase without writing this needless JavaScript code?

推荐答案

我想,如果我理解的核心问题,简单的答案是,有没有办法强迫一个空数组到火力地堡。然而,也有一些范式可能工作比你有什么更好的上面。

I think that, if I understand the core question, the short answer is that there is no way to force an empty array into Firebase. However, there are some paradigms that might work better than what you have above.

请记住,火力地堡是一个实时的环境。汽车和事故可以(将)随时变化的数量。这是最好的对待一切,实时到达的新数据,并避免甚至想着存在或不存在。

Keep in mind that Firebase is a real-time environment. The number of cars and accidents can (and will) change at any time. It's best to treat everything as new data arriving in real time and avoid even thinking about exists or doesn't exist.

// fetch all the people in real-time
rootRef.child('people').on('child_added', function(personSnapshot) {

   // monitor their cars
   personSnapshot.ref().child('cars', 'child_added', function(carSnapshot) {

       // monitor accidents
       carSnapshot.ref().child('accidents', 'child_added', function(accidentSnapshot) {
           // here is where you invoke your code related to accidents
       });
   });
});

请注意怎么也没有必要是否存在/除非键入逻辑。请注意,你可能也想监视 child_removed 汽车和呼叫 ref.off()来停止收听特定的儿童。

Note how there is no need for if exists/unless type logic. Note that you would probably also want to monitor child_removed on cars and people and call ref.off() to stop listening to specific children.

如果由于某种原因,你要坚持用静态模式,然后的forEach 将成为您的朋友:

If for some reason you want to stick with the static model, then forEach will become your friend:

// fetch all the people as one object, asynchronously
// this won't work well with many thousands of records
rootRef.child('people').once('value', function(everyoneSnap) {

   // get each user (this is synchronous!)
   everyoneSnap.forEach(function(personSnap) {

        // get all cars (this is asynchronous)
        personSnap.ref().child('cars').once('value', function(allCars) {

           // iterate cars (this is synchronous)
           allCars.forEach(function(carSnap) { /* and so on */ });

        });

   });   
});

请注意如何,甚至用foreach,没有必要的存在,或者除非之类的逻辑。

Note how, even with forEach, there is no need for "exists or unless" sort of logic.

这篇关于如何处理空数组在火力地堡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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