Javascript 如何操作firebase快照数据

Javascript 如何操作firebase快照数据,javascript,firebase,firebase-realtime-database,Javascript,Firebase,Firebase Realtime Database,我试图了解如何删除快照中的节点。我看了其他例子,但没有发现关于删除的内容 这就是JSON的样子 { "series": { "audit": null, "contacts": null, "images": null, "products": { "-LQgKSWzEtN9eVHcVLzV": { "dataEntryDevice": "FIGGG", "dataEntryDeviceVersion": "

我试图了解如何删除快照中的节点。我看了其他例子,但没有发现关于删除的内容

这就是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()
        }
      })
    })
  }
})

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

const filteredProducts = x.val().products.filter(p => !p.id)
然后使用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文档通常建议不要将数组用于此类列表结构。出于某些原因,请参阅如何获取productUIDx。键应该是您的productUID