如何操作Firebase快照数据

时间:2018-11-17 20:27:42

标签: javascript firebase firebase-realtime-database

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

这就是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的任何元素。我尝试了一些不同的方法,但是找不到解决方案。这是我尝试过的最后一次。

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

1 个答案:

答案 0 :(得分:2)

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

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

,然后使用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)
    })
  }
})

完整解决方案:

{{1}}