【问题标题】:add, set, update document with geofirestore使用 geofirestore 添加、设置、更新文档
【发布时间】:2019-07-29 18:52:09
【问题描述】:

我正在尝试将 geofirestore https://geofirestore.com/ 与 react native 一起使用,事实是我已经实现了在 firestore 中添加元素的方法,但是我在文档中读到必须通过 geofirestore 方法添加数据。我是否必须更改所有方法并使用 firestore 方法?我使用 javascript 而不是 typeScript,我看到有一些带有 typescript 的示例,似乎他们已经更新了库,而其他示例不起作用。 如何在 bbdd 中添加元素? 我使用了geofirex,它很简单,但没有限制,我想换成geofirestore才能使用这个功能。 谢谢!

【问题讨论】:

    标签: reactjs react-native google-cloud-firestore geospatial geofirestore


    【解决方案1】:

    所以大多数(如果不是全部)Typescript 示例几乎与您在 JS 中所做的相同(以及常规的 firebase 库)。给大家举个例子,在react-native-firebase中添加查询(有限制)。

    import firebase from 'react-native-firebase';
    
    const doc = {
       coordinates: new firebase.firestore.GeoPoint(0, 0),
       name: 'The center of the WORLD'
    };
    
    const collection = firebase.firestore().collection('todos');
    
    collection.add(doc).then((docRef) => {
       collection.limit(100).get().then((querySnapshot) => {
          // 100 items in query
          querySnapshot.docChanges.forEach((change) => {
             console.log(change.doc);
          });
       });
    });
    

    现在我从未使用过 RNF,但从文档中我可以得出这是正确的,并且它们几乎所有的事情都与 JS Firebase 库相同(除了 docChanges 作为数组返回而不是作为函数返回数组...)。不管怎样,让我们​​在 Geofirestore 中看到同样的东西,还有使用limitnear 查询位置的额外好处!

    import firebase from 'react-native-firebase';
    import { GeoFirestore } from 'geofirestore';
    
    const doc = {
       coordinates: new firebase.firestore.GeoPoint(0, 0),
       name: 'The center of the WORLD'
    };
    
    const geofirestore = new GeoFirestore(firebase.firestore());
    const geocollection = geofirestore.collection('todos');
    
    geocollection.add(doc).then((docRef) => {
       geocollection.limit(100).near({
          center: new firebase.firestore.GeoPoint(0, 0),
          radius: 10
       }).get().then((querySnapshot) => {
          // 100 items in query within 10 KM of coordinates 0, 0
          querySnapshot.docChanges().forEach((change) => {
             console.log(change.doc);
          });
       });
    });
    

    不管怎样,不要害怕 Typescript 代码示例,如果你只是去掉 : GeoFirestore 或其他任何有效的 JS...

    // This in TS
    const firestore = firebase.firestore();
    const geofirestore: GeoFirestore = new GeoFirestore(firestore);
    
    // Is this in JS
    const firestore = firebase.firestore();
    const geofirestore = new GeoFirestore(firestore);
    

    最后,如果有帮助的话,我会尝试使用 Vanilla JS 让这个 viewers app 保持最新。

    【讨论】:

    • 非常感谢@MichaelSolati,我已经明白了很多。问题是我前面已经和firestore连接了,文档中还有另一个结构,现在g:g,l:l,d:d不同了,但是一切都通过一点点努力就解决了...... A问题,如果我想修改文档,删除...我还必须使用 geofirestore 或者我可以使用更新、设置、删除 firestore 方法?示例集:await firestore.set (users / $ {user.uid}, {update: update}, {merge: true}) 再次感谢您提供这个精彩的库 :)
    • 您可以在不使用 geofirestore 的情况下直接更新(包括删除)文档,只需确保使用 d. 对您的字段或对象进行子注释。但是,如果您更新位置/坐标,您应该使用 geofirestore 进行更新。
    • 谢谢,我了解它是如何工作的,并且我正在逐步在我的项目中实现它......你能告诉我如何完成相同的查询,但要打开监听事件吗?就像我说的,我正在使用带有 firebase 的 react-native,但连接到 web sdk,而不是 RNFirebase 库。再次问候和感谢
    • RNF 库尝试成为 web sdk 的 1-2-1 复制。几乎每个类和方法的行为都是一样的。
    【解决方案2】:

    这很好用

    等待 eventRef.update({'d.arrUsers': firebase.firestore.FieldValue.arrayUnion(userUid)});

    但是我无法完成这项工作,它是一张地图,我想在它正常工作之前添加另一个用户......

    等待 eventRef.set({'d.userMap': { [用户ID]:{ 姓名:姓名, 年龄:年龄 } }},{合并:真});

    如果我使用更新,它会删除存在的那个并放入新的,如果我这样做,它会从文档中的 d 中取出它

    【讨论】:

    • 如果您正在使用 geofirestore 并且正在更新/设置地理文档,则不要将 d. 放在对象前面。太好了……await eventRef.update({'arrUsers': firebase.firestore.FieldValue.arrayUnion(userUid)});await eventRef.set({ userMap: { [userUid]: { name: name, age: age } }}, {merge: true});
    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 2017-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多