问题
I have a firebase function performing a query and filtering by a geohash search radius. It's working but I am returning Json and what I really want to do is return a snapshot. I am new to firebase functions.
Here is my firebase function
var GeoFirestore = require('geofirestore').GeoFirestore;
exports.geoQueryWitningRadius = functions.https.onRequest(async (req, res) => {
const data = req.body;
const lat = data.lat;
const lon = data.lon;
const radius = data.radius;
const firestore = admin.firestore();
const geofirestore = new GeoFirestore(firestore);
const geocollection = geofirestore.collection('pickups');
const query = geocollection.near({ center: new admin.firestore.GeoPoint(lat, lon), radius:
radius });
await query.get().then((snapshot) => {
return res.send(snapshot)
}).catch(error => {
console.log('error', error);
});
});
Here is my http request in swift
static func getGeoHashPickups(radius: Int, completion: @escaping([String: Any]?, String?) -> Void) { //url, Error
let parameters : [String : Any] = [
"lat" : UserService.user.lat,
"lon" : UserService.user.lon,
"radius" : radius
]
let url = "https://us-central1-app-request-ect"
AF.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: [:]).responseJSON { response in
switch response.result {
case .success(let dict):
guard let dictionary = dict as? [String : Any] else { return }
completion(dictionary, nil)
case .failure(let error):
print(error.localizedDescription)
completion(nil, error.localizedDescription)
}
}
}
How do I convert what comes back to my front end to a snapshot?
回答1:
Cloud Functions cannot send back a DocumentSnapshot
as that type is not serializable. And there is no way to convert the JSON back to a DocumentSnapshot
in the Swift code.
But what you get boils down to the data of that snapshot, which should be enough. If you're missing something in the data returned, you'll have to also return that additional information from your Cloud Functions code, so the Swift code can pick it up.
来源:https://stackoverflow.com/questions/65110141/how-firebase-http-function-to-return-snapshot-to-swift-app