繁体   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