生成随机密钥火库

eee*_*ttt 4 javascript key firebase google-cloud-firestore

我正在运行一个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)
Run Code Online (Sandbox Code Playgroud)

gia*_*min 5

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

const uid = admin.firestore().collection("tmp").doc().id
Run Code Online (Sandbox Code Playgroud)

这将在不需要保存一些数据的情况下做到这一点

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

await admin.firestore().collection("MY COLLECTION").doc().set(data)
Run Code Online (Sandbox Code Playgroud)


Wil*_*hou 1

看起来你可以只使用 .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);
});
Run Code Online (Sandbox Code Playgroud)