我在我的代码中运行了一个类似的问题。
我需要创建一个随机密钥,所以我找到了this code(由客户端的firestore使用):
const chars'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
let autoId = ''
for (let i = 0; i < 20; i++) {
autoId += chars.charAt(Math.floor(Math.random() * chars.length))
}
检查您的用户是否来自纽约
If (myUserIsFromNy) {
let ny = "NY ";
let res = ny.concat(autoId);
}
autoId 是 firestore 在 .add() 方法上生成的 id。
相反,您可以创建自己的函数并将其存储在变量中,
let key = myKeyFunction()
一个好主意是:
const chars'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
let autoId = ''
for (let i = 0; i < 5; i++) {
autoId += chars.charAt(Math.floor(Math.random() * chars.length))
}
执行一个函数来查找您的用户所在的位置并获取来自城市的前两个字母。
首先,您可以检查
https://www.w3schools.com/html/html5_geolocation.asp
通过纬度和经度,您可以创建一个函数来查找它的位置、城市等......
或者您可以通过在注册表格中询问来简化操作。
然后:
let ny = "NY-"
let key = ny.concat(autoId)
您可以使用以下方法创建文档:
db.ref.doc(key).set(Data)
Data 是您的用户信息
运行云功能来执行此操作是一个糟糕的解决方案,因为服务器将对每个新用户进行额外的写入,您应该只运行 1 次写入并将运行两次,这将增加使用 firestore 的成本,firestore 会强制您执行最佳实践,因为如果不这样做,您将为此付费。