Geofirestore 云函数 w/Cors,查询 Promise NodeJS 问题
Geofirestore Cloud Function w/Cors, Issue with Query Promise NodeJS
这是一个 Google 在以 Geofirestore 方式添加文档后调用 Geofirestore 的云函数 https://github.com/geofirestore/geofirestore-js
我有一件物品:
我得到了 {"sendit":{"domain":{"domain":null,"_events":{},"_eventsCount":1,"members":[]}} }
然而,
I can't get it from query when the res.send({sendit}), is sent
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
});
}}
also asked here: I'll update both https://github.com/geofirestore/geofirestore-js/issues/154
From the Geofirestore developer 也有针对 Cloud Functions 的讨论和工作 CORS
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const GeoFirestore = require('geofirestore').GeoFirestore;
admin.initializeApp();
const firestoreRef = admin.firestore();
const geofirestore = new GeoFirestore(firestoreRef);
const geocollection = geofirestore.collection('planner');
exports.chooseCity = functions.https.onRequest(async (request, response) => {
if (request.method === 'OPTIONS') {
response.set('Access-Control-Allow-Methods', 'POST');
return response.status(200).send({});
} else {
const radius = request.body.distance;
const center = new admin.firestore.GeoPoint(request.body.location[0],
request.body.location[1]);
// The data from a doc is returned as a function,
// so you just need to map and call the function
// sendit returns a single promise
const sendit = (await geocollection.near({ center, radius })
.get())
.docs
.map((doc) => ({ ...doc, data: doc.data() }));
return response.status(200).send({ sendit });
}
});
这是一个 Google 在以 Geofirestore 方式添加文档后调用 Geofirestore 的云函数 https://github.com/geofirestore/geofirestore-js
我有一件物品: 我得到了 {"sendit":{"domain":{"domain":null,"_events":{},"_eventsCount":1,"members":[]}} } 然而,
I can't get it from query when the res.send({sendit}), is sent
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
});
}}
also asked here: I'll update both https://github.com/geofirestore/geofirestore-js/issues/154
From the Geofirestore developer 也有针对 Cloud Functions 的讨论和工作 CORS
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const GeoFirestore = require('geofirestore').GeoFirestore;
admin.initializeApp();
const firestoreRef = admin.firestore();
const geofirestore = new GeoFirestore(firestoreRef);
const geocollection = geofirestore.collection('planner');
exports.chooseCity = functions.https.onRequest(async (request, response) => {
if (request.method === 'OPTIONS') {
response.set('Access-Control-Allow-Methods', 'POST');
return response.status(200).send({});
} else {
const radius = request.body.distance;
const center = new admin.firestore.GeoPoint(request.body.location[0],
request.body.location[1]);
// The data from a doc is returned as a function,
// so you just need to map and call the function
// sendit returns a single promise
const sendit = (await geocollection.near({ center, radius })
.get())
.docs
.map((doc) => ({ ...doc, data: doc.data() }));
return response.status(200).send({ sendit });
}
});