我想在Firestore中创建一个带有唯一ID和名称的文档。为此,我想创建与Firebase Firestore唯一ID相同类型的唯一ID。
- 如何创建带有自动生成的Firestore ID的唯一ID?
- Firebase Admin SDK中是否有相关函数?
更新:
正如Frank van Puffelen在评论中提到的那样,AutoId是我正确的选择!
这是我在服务器端使用Node.js时使用的函数:
function generateUniqueFirestoreId(){
// Alphanumeric characters
const chars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let autoId = '';
for (let i = 0; i < 20; i++) {
autoId += chars.charAt(Math.floor(Math.random() * chars.length));
}
return autoId;
}
AuthId.newId()
。参见 https://dev59.com/AKjja4cB1Zd3GeqP8EOH#48284468 - Frank van Puffelenconst FirestoreAutoId = () => { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; return Array.from({ length: 20 }).reduce((acc) => \
${acc}${chars.charAt(Math.floor(Math.random() * chars.length))}`); };` - Ale TheFe