How can I get the id of a inserted document firebase-admin

Viewed 77

I am handling some documents with firebase realtime database. I need to delete a document that I don't have the access to read it client side via SDK. In order to do that I need to know what is the id, so I should store it in my db (mongo) and when I need to delete a document on firebase I just get it from my DB and the I delete it.

I took a look to this answer but it doesn't work for me.

I insert documents into my firebase DB with this method (server side using firebase-admin)

const writeNotification = (uid: string, notification: INotification) => {
  const db = admin.database();
  const notsRef = db.ref(`notifications/${uid}`);
  notsRef.push({
    ..._.omit(notification, 'to'),
  });
};

If I do notsRef.id I get undefined.

How can I get the ID of my document that I have just inserted?

p.s. my documents are organized like this:

enter image description here

1 Answers

The answer you are looking at is about Firestore and not Realtime Database that you are using. The Reference has a key property and not id:

console.log('New Key', notsRef.key) 

// to get child node's key
const childNotesRef = notsRef.push({
    ..._.omit(notification, 'to'),
  });

console.log(childNotesRef.key)
Related