首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用geofirestore添加、设置和更新文档

使用geofirestore添加、设置和更新文档
EN

Stack Overflow用户
提问于 2019-03-08 03:22:14
回答 2查看 1.8K关注 0票数 2

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

EN

回答 2

Stack Overflow用户

发布于 2019-03-08 14:47:08

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

代码语言:javascript
复制
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查询位置的好处!

代码语言:javascript
复制
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...

代码语言:javascript
复制
// 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保持最新,如果这有帮助的话。

票数 4
EN

Stack Overflow用户

发布于 2019-03-10 06:06:22

这个很好用

firebase.firestore.FieldValue.arrayUnion(userUid)});等待用户({‘d.arrUsers’:

eventRef.update

然而,我无法使此工作,这是一个地图,我想添加另一个用户,在它正常工作之前…

等待eventRef.set({'d.userMap':{ userUid:{姓名,年龄:年龄}),{merge: true});

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55051357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档