【问题标题】:Should I do GeoFirestore transactions in Cloud Functions or Client-side?我应该在 Cloud Functions 还是客户端进行 GeoFirestore 事务?
【发布时间】:2022-01-16 01:00:37
【问题描述】:

如果在给定坐标内不存在其他文档,我将使用此库 https://geofirestore.com/ 在集合中创建文档。现在,我应该在 Cloud 函数中还是直接在应用程序中使用这个库的方法进行交易?

我个人认为没有理由在云端执行此操作,但我可能错了,所以我征求您的意见。

const geocollection = GeoFirestore.collection('chats');
const query = geocollection.limit(1).near({ center: new firebase.firestore.GeoPoint(latitude, longitude), radius: 0.03 });

GeoFirestore.runTransaction((transaction) => {
  const geotransaction = new GeoTransaction(transaction);
  return geotransaction.get(query).then((sfDoc) => {
    if (sfDoc.exists) {
      sfDoc.docs.forEach(documentSnapshot => {
        if (documentSnapshot.exists) {
          firestore().collection('chats')
            .doc(documentSnapshot.id)
            .get()
            .then((res) => {                  
                // insert new document
                geocollection.add({
                  name: 'Geofirestore',
                  uid: device_id,                     
                  coordinates: new firebase.firestore.GeoPoint(latitude, longitude),
                  created: currenttime
                })                  
            })
        }
      });
    } else {
      geocollection.add({
        name: 'Geofirestore',
        uid: device_id,           
        coordinates: new firebase.firestore.GeoPoint(latitude, longitude),
        created: currenttime
      })
    }
  });
});

【问题讨论】:

    标签: javascript react-native google-cloud-firestore google-cloud-functions geofirestore


    【解决方案1】:

    如果您希望文档的位置在其集合中是唯一的,请考虑使用该位置作为文档 ID 的基础,或从该位置派生的某个值(例如哈希或 Geofirestore 使用的 geohash) .

    【讨论】:

    • 嗯,从来没想过。如果其他人想在该半径内创建一个文档,那会不会创建另一个唯一的哈希?还是哈希值相同?
    • 关键应该是你想要唯一的东西。 Geohashes 可以具有任意精度,因此您可以匹配您的要求,但其他方案也是可能的。重要的是,ID 几乎是在存在潜在不良行为者的情况下保证唯一性的唯一方法。参见stackoverflow.com/questions/47543251/…stackoverflow.com/questions/47405774/… 以及来自google.com/search?q=%5Bgoogle-cloud-firestore%5D+unique+value 的更多信息
    • 我不确定我是否做对了。所以我可以为给定的点半径创建一个唯一键,这样其他人就不能在该位置创建文档?
    • 我想你可能有点误解了这个问题。我在那里使用30米的半径。所以基本上我不希望两个用户在该半径内创建文档。应允许一个用户在 30 米半径范围内创建文档。创建它们时,两个文档的 Geohash 不会相同。因此我不能将它们用作约束。
    猜你喜欢
    • 2011-12-06
    • 2014-05-10
    • 2012-05-30
    • 2012-09-22
    • 2019-11-20
    • 1970-01-01
    • 2015-12-10
    • 2012-01-12
    • 1970-01-01
    相关资源
    最近更新 更多