Ad

Not Able To Get Last Insert Id Of Data In Firebase

I need help to get last insert id of the data, when i do insert record, I tried some code but it is not working for me, here i have added my code, can anyone please check my code and help me whats issue in it

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);
Ad

Answer

To be able to get the id try the following:

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

First you store the random id inside a variable, then you use that variable inside child() and add it to the database. Therefore, organization_id will be the last inserted id in the database.

Ad
source: stackoverflow.com
Ad