所以大多数(如果不是全部)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 中看到同样的东西,还有使用limit 和near 查询位置的额外好处!
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 保持最新。