这是一个 Google Cloud Function 在添加文档后调用 Geofirestore 的 Geofirestore 方式https://github.com/geofirestore/geofirestore-js
我有一个项目:我得到了 {"sendit":{"domain":{"domain":null,"_events":{},"_eventsCount":1,"members":[]}}} 但是,
发送 res.send({sendit}) 时,我无法从查询中获取它
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const GeoFirestore = require("geofirestore").GeoFirestore;
//const firestore = require('firebase/firestore');
const cors = require("cors")({
origin: true,
allowedHeaders: [
"Access-Control-Allow-Origin",
"Access-Control-Allow-Methods",
"Content-Type",
"Origin",
"X-Requested-With",
"Accept",
"Access-Control-Allow-Headers"
],
methods: ["POST", "OPTIONS"]
});
require('firebase/firestore');
//serviceAccount=yourServiceAccountKey.json
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "your_firebaseio_domain"
})
const firestoreRef = admin.firestore();
const geofirestore = new GeoFirestore(firestoreRef);
const geocollection = geofirestore.collection("planner");
exports.chooseCity = functions.https.onRequest((req, res) => {
// Google Cloud Function res.methods
res.set("Access-Control-Allow-Headers", "Content-Type");
res.set("Content-Type", "Application/JSON");
// CORS-enabled req.methods, res.methods
return cors(req, res, () => {
res.set("Access-Control-Allow-Headers", "Content-Type");
res.set("Content-Type", "Application/JSON");
var origin = req.get("Origin");
var allowedOrigins = [
"https://yourdomain.tld"
];
if (allowedOrigins.indexOf(origin) > -1) {
// Origin Allowed!!
res.set("Access-Control-Allow-Origin", origin);
if (req.method === "OPTIONS") {
// Method accepted for next request
res.set("Access-Control-Allow-Methods", "POST");
//SEND or end # option req.method
return res.status(200).send({});
} else {
// After request req.method === 'OPTIONS'
if (req.body) {
const radius = req.body.distance;
const center = new admin.firestore.GeoPoint(req.body.location[0], req.body.location[1])
const geoQuery = geocollection.near({ center, radius });
[**** Tried this ****]
let results = []
// Remove documents when they fall out of the query
geoQuery.on('key_exited', ($key) => {
const index = results.findIndex((place) => place.$key === $key);
if (index >= 0) results.splice(index, 1);
});
// As documents come in, add the $key/id to them and push them into our results
geoQuery.on('key_entered', ($key, result) => {
result.$key = $key;
results.push(result);
});
return res.status(200).send({results})
[**** Instead of this ****]
geoQuery.get()
.then(value => {
// All GeoDocument returned by GeoQuery,
//like the GeoDocument added above
const sendit = value.docs
res.status(200).send({sendit})
[**End**]
})
.catch(err => res.status(400).send(err))
} else {
res.status(200).send("no request body");
}
}
} else {
//Origin Bad!!
//SEND or end
return res.status(400).send("no access for this origin");
}
});
});
在 redux 操作中添加文档,geofirestore 方式
import { GeoFirestore } from "geofirestore";
export const createEvent = event => {
return (dispatch, getState, { getFirebase, getFirestore }) => {
const firestore = getFirestore();
const geoFirestore = new GeoFirestore(firestore);
const geocollection = geoFirestore.collection("planner");
geocollection.add({
name: "Geofirestore",
score: 100,
// The coordinates field must be a GeoPoint!
coordinates: event.geopoint,
title: event.title,
body: event.body,
chosenPhoto: event.chosenPhoto,
date: event.date,
createdAt: event.createdAt,
updatedAt: event.updatedAt,
address: event.address,
location: event.location,
city: event.city,
etype: event.etype,
geopoint: event.geopoint
});
}}
还在这里问:我将同时更新https://github.com/geofirestore/geofirestore-js/issues/154