无法获取Firebase中数据的最后插入ID

时间:2019-07-12 07:08:54

标签: javascript firebase firebase-realtime-database

我需要帮助来获取数据的最后插入ID,当我插入记录时,我尝试了一些代码,但是对我来说不起作用,在这里我添加了我的代码,有人可以检查我的代码并帮助我吗?问题

const data = {
      //user_id: user_id,
      is_owner: 1,
      name: organization_name,
      website: website,
      industry: industry,
      viewing_permission: viewing_permission,
      plan: plan
    };
    const ref = db.ref("xpertz_organization/" + user_id);
    const organization_id = ref.push().key(); 
    ref.push().set(data);

1 个答案:

答案 0 :(得分:1)

要获取ID,请尝试以下操作:

const ref = db.ref("xpertz_organization/" + user_id);
const organization_id = ref.push().getKey();
ref.child(organization_id).set(data);

首先,将随机ID存储在变量中,然后在child()中使用该变量,并将其添加到数据库中。因此,organization_id将是数据库中最后插入的ID。