【问题标题】:Generating random Key firestore生成随机密钥火库
【发布时间】:2018-09-10 17:18:12
【问题描述】:

我正在运行一个 FIRESTORE 数据库,并且我想创建一个具有 与 firestore 相同的模式的随机密钥。

在链接中,我找到了创建文档后调用的函数 使用:'db.ref.add()' 在客户端生成密钥:

https://github.com/firebase/firebase-js-sdk/blob/73a586c92afe3f39a844b2be86086fddb6877bb7/packages/firestore/src/util/misc.ts#L36

我需要这样做:

let key = newId()
console.log(key)
db.ref().doc(key).set(data)

【问题讨论】:

    标签: javascript firebase key google-cloud-firestore


    【解决方案1】:

    firestore uid 生成器非常容易使用:

    const uid = admin.firestore().collection("tmp").doc().id
    

    这样就可以解决问题而无需保存一些数据

    但在您的具体示例中:如果您不指定任何密钥,它将由 firestore 自动生成:

    await admin.firestore().collection("MY COLLECTION").doc().set(data)
    

    【讨论】:

      【解决方案2】:

      看起来您可以只使用 .add 方法,而不是自己自动生成 uuid。

      https://firebase.google.com/docs/firestore/manage-data/add-data

      // Add a new document with a generated id.
      db.collection("cities").add({
          name: "Tokyo",
          country: "Japan"
      })
      .then(function(docRef) {
          console.log("Document written with ID: ", docRef.id);
      })
      .catch(function(error) {
          console.error("Error adding document: ", error);
      });
      

      【讨论】:

        猜你喜欢
        • 2015-09-27
        • 1970-01-01
        • 2012-11-15
        • 2014-07-20
        • 1970-01-01
        • 2013-06-23
        • 2018-01-12
        • 1970-01-01
        • 2012-05-21
        相关资源
        最近更新 更多