簡體   English   中英

如何獲取插入文檔的 ID firebase-admin

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

我正在使用 firebase 實時數據庫處理一些文檔。 我需要刪除一個我無權通過 SDK 在客戶端讀取它的文檔。為了做到這一點,我需要知道 id 是什么,所以我應該將它存儲在我的數據庫(mongo)中,當我需要刪除 firebase 上的文檔,我只是從我的數據庫中獲取它,然后將其刪除。

我看了一下這個答案,但它對我不起作用。

我使用這種方法將文檔插入到我的 firebase 數據庫中(服務器端使用firebase-admin

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

如果我不這樣做notsRef.id我得到undefined

如何獲取我剛剛插入的文檔的 ID?

ps我的文檔是這樣組織的:

在此處輸入圖像描述

您正在查看的答案是關於Firestore而不是您正在使用的實時數據庫 Reference有一個key屬性而不是id

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

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

console.log(childNotesRef.key)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM