更新Firebase函数中没有id的对象子对象

更新Firebase函数中没有id的对象子对象,firebase,firebase-realtime-database,google-cloud-functions,Firebase,Firebase Realtime Database,Google Cloud Functions,希望你能帮助我 在firebase数据库函数的触发器中,我尝试更新 不知道id的子对象 因此,我想通过只知道categoria元素的名称而不知道产品名称来更新categoria元素 请帮助…您可以先检索分类数据。假设您知道第一个键LNBXRLPR0OY8-\u Cnm,并且只有在多个categoria将用另一个代码快照解释时,您才有一个categoria项 firebase.database().ref('product/' + key + '/categoria').once('value',

希望你能帮助我

在firebase数据库函数的触发器中,我尝试更新 不知道id的子对象

因此,我想通过只知道categoria元素的名称而不知道产品名称来更新categoria元素


请帮助…

您可以先检索分类数据。假设您知道第一个键LNBXRLPR0OY8-\u Cnm,并且只有在多个categoria将用另一个代码快照解释时,您才有一个categoria项

firebase.database().ref('product/' + key + '/categoria').once('value', snapshot=>{
  if (snapshot.exists()) var categoriaKey = Object.keys(snapshot.val()[0])
  firebase.database().ref('product/' + key + '/' + categoriaKey).set(newCategoriaObject)
})
编辑:

productList={}
firebase.database().ref('product').once('value', snap=>{
   snap.forEach(p=>{
      productList[p.key]=p.val().name; 
   })
}) 
使用上面的产品列表对象数组。场景:您需要向用户显示产品名称列表。用户选择产品后,您可以使用以下功能检索密钥

function findKey(productList, selectedProductName) { 
     for (let key in productList)
        if (productList[key] === selectedProductName) return key;
}

key = findKey(productList, selectedProductName);

因此,使用上述简单代码,您将获得用户选择的产品密钥。如果您没有其他场景:

如果我不知道产品密钥怎么办?那么您将以与上面显示的代码相同的方式检索产品密钥。在这个时候你会有一把钥匙。您可以使用forEach方法循环所有产品密钥。因此,首先您需要了解产品密钥OK,我如何在firebase函数中获取密钥?您需要每个产品id或描述,以便能够选择要更新的产品。所以您将有两个步骤。第一步。您将检索产品密钥并从其id中选择产品,然后检索此产品的类别。我会更新上面的代码。完全有效!!谢谢