如何操作Firebase快照数据 [英] How to manipulate firebase snapshot data

查看:47
本文介绍了如何操作Firebase快照数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何删除快照内的节点.我看过其他示例,但删除后未找到任何内容.

I am trying to understand how to delete a node inside a snapshot. I have looked at other examples but I did not find something on deleting.

这就是JSON的样子

    {
  "series": {
    "audit": null,
    "contacts": null,
    "images": null,
    "products": {
      "-LQgKSWzEtN9eVHcVLzV": {
        "dataEntryDevice": "FIGGG",
        "dataEntryDeviceVersion": "1.0",
        "dateTimeUploaded": "2018-11-06T20:02:20-08:00",
        "marketId": 19,
        "metrostudyMarketId": "D245B426-102C-4678-BED2-BB9FB35764BD",
        "products": [
          {
            "auditDate": "11/06/2018",
            "bathrooms": "",
            "bedrooms": "",
            "bestSeller": false,
            "garage": "",
            "id": "",
            "model": false,
            "name": "",
            "newProduct": false,
            "notAvailable": false,
            "price": 0,
            "stories": "",
            "unitsReleased": 0,
            "unitsSold": 0
          },
          {
            "auditDate": "11/06/2018",
            "bathrooms": "2.5",
            "bedrooms": "3",
            "bestSeller": false,
            "garage": "",
            "id": "AFF1EE66-578C-4DA0-90A2-B9F447EA52B6",
            "model": false,
            "name": "Plan 2",
            "newProduct": true,
            "notAvailable": true,
            "price": 0,
            "stories": 2,
            "unitsReleased": 4,
            "unitsSold": 4
          },
          {
            "auditDate": "11/06/2018",
            "bathrooms": "3",
            "bedrooms": "3",
            "bestSeller": false,
            "garage": "",
            "id": "0059ED00-102C-48D9-95E0-E68A04D2E877",
            "model": false,
            "name": "Plan 5",
            "newProduct": true,
            "notAvailable": true,
            "price": 0,
            "stories": 2,
            "unitsReleased": 3,
            "unitsSold": 3
          },
          {
            "auditDate": "11/06/2018",
            "bathrooms": "4",
            "bedrooms": "5",
            "bestSeller": false,
            "garage": "",
            "id": "08CEF557-EDF7-46E7-AC9B-A380E2046616",
            "model": false,
            "name": "Plan 6",
            "newProduct": true,
            "notAvailable": true,
            "price": 0,
            "stories": 2,
            "unitsReleased": 2,
            "unitsSold": 2
          }
        ],
        "seriesId": "5571E356-AEE5-443A-AC45-47661EB99096",
        "userId": "395a2c10-6d7e-41d9-92cd-0704caf5eb26"
      },
      "Optional(\"-LQgHPWQ1KKElA8zWIKv\")": {
        "auditorUserId": "395a2c10-6d7e-41d9-92cd-0704caf5eb26",
        "dataEntryDevice": "iPad",
        "dataEntryDeviceVersion": "0.9906",
        "dateTimeUploaded": "2018-11-06T19:49:02",
        "marketId": "19",
        "metrostudyMarketId": "D245B426-102C-4678-BED2-BB9FB35764BD",
        "products": [
          {
            "auditDate": "2018-11-06T19:49:02",
            "bathrooms": "2.5",
            "bedrooms": "3",
            "bestSeller": false,
            "finishedSqFt": 1796,
            "garagesNumber": "2",
            "id": "AFF1EE66-578C-4DA0-90A2-B9F447EA52B6",
            "model": false,
            "name": "Plan 2",
            "newProduct": true,
            "notAvailable": false,
            "stories": 2
          }
        ],
        "seriesId": "5571E356-AEE5-443A-AC45-47661EB99096",
        "userId": "395a2c10-6d7e-41d9-92cd-0704caf5eb26",
        "userName": "Lkolokousis@metrostudy.com"
      },
      "Optional(\"-LQgHY12QubrBiwU0Uw7\")": {
        "auditorUserId": "395a2c10-6d7e-41d9-92cd-0704caf5eb26",
        "dataEntryDevice": "iPad",
        "dataEntryDeviceVersion": "0.9906",
        "dateTimeUploaded": "2018-11-06T19:49:36",
        "marketId": "19",
        "metrostudyMarketId": "D245B426-102C-4678-BED2-BB9FB35764BD",
        "products": [
          {
            "auditDate": "2018-11-06T19:49:02",
            "bathrooms": "2.5",
            "bedrooms": "3",
            "bestSeller": false,
            "finishedSqFt": 1796,
            "garagesNumber": "2",
            "id": "AFF1EE66-578C-4DA0-90A2-B9F447EA52B6",
            "model": false,
            "name": "Plan 2",
            "newProduct": true,
            "notAvailable": false,
            "stories": 2
          },
          {
            "auditDate": "2018-11-06T19:49:36",
            "bathrooms": "3",
            "bedrooms": "3",
            "bestSeller": false,
            "finishedSqFt": 2438,
            "garagesNumber": "2",
            "id": "0059ED00-102C-48D9-95E0-E68A04D2E877",
            "model": false,
            "name": "Plan 5",
            "newProduct": true,
            "notAvailable": false,
            "stories": 2
          }
        ],
        "seriesId": "5571E356-AEE5-443A-AC45-47661EB99096",
        "userId": "395a2c10-6d7e-41d9-92cd-0704caf5eb26",
        "userName": "Lkolokousis@metrostudy.com"
      },
      "Optional(\"-LQgHeGVu075fgcbBWP7\")": {
        "auditorUserId": "395a2c10-6d7e-41d9-92cd-0704caf5eb26",
        "dataEntryDevice": "iPad",
        "dataEntryDeviceVersion": "0.9906",
        "dateTimeUploaded": "2018-11-06T19:50:06",
        "marketId": "19",
        "metrostudyMarketId": "D245B426-102C-4678-BED2-BB9FB35764BD",
        "products": [
          {
            "auditDate": "2018-11-06T19:49:02",
            "bathrooms": "2.5",
            "bedrooms": "3",
            "bestSeller": false,
            "finishedSqFt": 1796,
            "garagesNumber": "2",
            "id": "AFF1EE66-578C-4DA0-90A2-B9F447EA52B6",
            "model": false,
            "name": "Plan 2",
            "newProduct": true,
            "notAvailable": false,
            "stories": 2
          },
          {
            "auditDate": "2018-11-06T19:49:36",
            "bathrooms": "3",
            "bedrooms": "3",
            "bestSeller": false,
            "finishedSqFt": 2438,
            "garagesNumber": "2",
            "id": "0059ED00-102C-48D9-95E0-E68A04D2E877",
            "model": false,
            "name": "Plan 5",
            "newProduct": true,
            "notAvailable": false,
            "stories": 2
          },
          {
            "auditDate": "2018-11-06T19:50:06",
            "bathrooms": "4",
            "bedrooms": "5",
            "bestSeller": false,
            "finishedSqFt": 2753,
            "garagesNumber": "2",
            "id": "08CEF557-EDF7-46E7-AC9B-A380E2046616",
            "model": false,
            "name": "Plan 6",
            "newProduct": true,
            "notAvailable": false,
            "stories": 2
          }
        ],
        "seriesId": "5571E356-AEE5-443A-AC45-47661EB99096",
        "userId": "395a2c10-6d7e-41d9-92cd-0704caf5eb26",
        "userName": "Lkolokousis@metrostudy.com"
      },
      "Optional(\"-LQgHf1zpDDXs7m7KVNk\")": {
        "auditorUserId": "395a2c10-6d7e-41d9-92cd-0704caf5eb26",
        "dataEntryDevice": "iPad",
        "dataEntryDeviceVersion": "0.9906",
        "dateTimeUploaded": "2018-11-06T19:50:09",
        "marketId": "19",
        "metrostudyMarketId": "D245B426-102C-4678-BED2-BB9FB35764BD",
        "products": [
          {
            "auditDate": "2018-11-06T19:50:09",
            "bathrooms": "2.5",
            "bedrooms": "3",
            "bestSeller": false,
            "finishedSqFt": 1796,
            "garagesNumber": "2",
            "id": "AFF1EE66-578C-4DA0-90A2-B9F447EA52B6",
            "model": false,
            "name": "Plan 2",
            "newProduct": true,
            "notAvailable": false,
            "stories": 2
          },
          {
            "auditDate": "2018-11-06T19:50:09",
            "bathrooms": "3",
            "bedrooms": "3",
            "bestSeller": false,
            "finishedSqFt": 2438,
            "garagesNumber": "2",
            "id": "0059ED00-102C-48D9-95E0-E68A04D2E877",
            "model": false,
            "name": "Plan 5",
            "newProduct": true,
            "notAvailable": false,
            "stories": 2
          },
          {
            "auditDate": "2018-11-06T19:50:09",
            "bathrooms": "4",
            "bedrooms": "5",
            "bestSeller": false,
            "finishedSqFt": 2753,
            "garagesNumber": "2",
            "id": "08CEF557-EDF7-46E7-AC9B-A380E2046616",
            "model": false,
            "name": "Plan 6",
            "newProduct": true,
            "notAvailable": false,
            "stories": 2
          }
        ],
        "seriesId": "5571E356-AEE5-443A-AC45-47661EB99096",
        "userId": "395a2c10-6d7e-41d9-92cd-0704caf5eb26",
        "userName": "Lkolokousis@metrostudy.com"
      },
      "Optional(\"-LQgImKX4q5l9A-sTnuH\")": {
        "auditorUserId": "395a2c10-6d7e-41d9-92cd-0704caf5eb26",
        "dataEntryDevice": "iPad",
        "dataEntryDeviceVersion": "0.9906",
        "dateTimeUploaded": "2018-11-06T19:55:01",
        "marketId": "19",
        "metrostudyMarketId": "D245B426-102C-4678-BED2-BB9FB35764BD",
        "products": [
          {
            "auditDate": "2018-11-06T19:55:01",
            "bathrooms": "2.5",
            "bedrooms": "3",
            "bestSeller": false,
            "finishedSqFt": 1796,
            "garagesNumber": "2",
            "id": "AFF1EE66-578C-4DA0-90A2-B9F447EA52B6",
            "model": false,
            "name": "Plan 2",
            "newProduct": true,
            "notAvailable": true,
            "stories": 2
          },
          {
            "auditDate": "2018-11-06T19:55:01",
            "bathrooms": "3",
            "bedrooms": "3",
            "bestSeller": false,
            "finishedSqFt": 2438,
            "garagesNumber": "2",
            "id": "0059ED00-102C-48D9-95E0-E68A04D2E877",
            "model": false,
            "name": "Plan 5",
            "newProduct": true,
            "notAvailable": true,
            "stories": 2
          },
          {
            "auditDate": "2018-11-06T19:55:01",
            "bathrooms": "4",
            "bedrooms": "5",
            "bestSeller": false,
            "finishedSqFt": 2753,
            "garagesNumber": "2",
            "id": "08CEF557-EDF7-46E7-AC9B-A380E2046616",
            "model": false,
            "name": "Plan 6",
            "newProduct": true,
            "notAvailable": true,
            "stories": 2
          }
        ],
        "seriesId": "5571E356-AEE5-443A-AC45-47661EB99096",
        "userId": "395a2c10-6d7e-41d9-92cd-0704caf5eb26",
        "userName": "Lkolokousis@metrostudy.com"
      },
      "Optional(\"-LQgL7xI2ZcYLV6LLXCO\")": {
        "auditorUserId": "395a2c10-6d7e-41d9-92cd-0704caf5eb26",
        "dataEntryDevice": "iPad",
        "dataEntryDeviceVersion": "0.9906",
        "dateTimeUploaded": "2018-11-06T20:05:18",
        "marketId": "19",
        "metrostudyMarketId": "D245B426-102C-4678-BED2-BB9FB35764BD",
        "products": [
          {
            "auditDate": "2018-11-06T20:05:18",
            "bathrooms": "2.5",
            "bedrooms": "3",
            "bestSeller": false,
            "finishedSqFt": 1796,
            "garagesNumber": "2",
            "id": "AFF1EE66-578C-4DA0-90A2-B9F447EA52B6",
            "model": false,
            "name": "Plan 2",
            "newProduct": true,
            "notAvailable": true,
            "stories": 2,
            "unitsReleased": 4,
            "unitsRemaining": 0,
            "unitsSold": 4
          },
          {
            "auditDate": "2018-11-06T20:05:18",
            "bathrooms": "3",
            "bedrooms": "3",
            "bestSeller": false,
            "finishedSqFt": 2438,
            "garagesNumber": "2",
            "id": "0059ED00-102C-48D9-95E0-E68A04D2E877",
            "model": false,
            "name": "Plan 5",
            "newProduct": true,
            "notAvailable": true,
            "stories": 2,
            "unitsReleased": 3,
            "unitsRemaining": 0,
            "unitsSold": 3
          },
          {
            "auditDate": "2018-11-06T20:05:18",
            "bathrooms": "4",
            "bedrooms": "5",
            "bestSeller": false,
            "finishedSqFt": 2753,
            "garagesNumber": "2",
            "id": "08CEF557-EDF7-46E7-AC9B-A380E2046616",
            "model": false,
            "name": "Plan 6",
            "newProduct": true,
            "notAvailable": true,
            "stories": 2,
            "unitsReleased": 2,
            "unitsRemaining": 0,
            "unitsSold": 2
          }
        ],
        "seriesId": "5571E356-AEE5-443A-AC45-47661EB99096",
        "userId": "395a2c10-6d7e-41d9-92cd-0704caf5eb26",
        "userName": "Lkolokousis@metrostudy.com"
      }
    }
  },
  "subdivision": null,
  "contact": null,
  "salescenter": null
}

我需要遍历嵌套产品数组并删除具有空ID的任何元素.我尝试了一些不同的方法,但是找不到解决方案.这是我尝试过的最后一次.

I need to loop through the nested products array and delete any element that has an empty id. I have tried some different things but cant find solution. Here is the last thign I tried.

 _seriesMaster.forEach((child) => {
  if (child.key === 'products') {
    child.forEach(x => {
      x.val().products.forEach((p, i) => {
        if (!p.id) {
          x.val().products[i].remove()
        }
      })
    })
  }
})

推荐答案

您将其视为数组,因此首先对其进行过滤:

You treat it as an array, so first filter it:

const filteredProducts = x.val().products.filter(p => !p.id)

,然后使用filteredProducts设置节点

and then set the node with filteredProducts

const productUID = x.key;
const path = 'series/products/' + productUID + '/products';
firebase.database().ref(path).set(filteredProducts)

完整解决方案:

_seriesMaster.forEach((child) => {
  if (child.key === 'products') {
    child.forEach(x => {
      const productUID = x.key;
      const filteredProducts = x.val().products.filter(p => !p.id)
      const path = 'series/products/' + productUID + '/products';
      firebase.database().ref(path).set(filteredProducts)
    })
  }
})

这篇关于如何操作Firebase快照数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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