I have a cloud function which is being executed successfully but it only returns a null value as a response to my app. In Firesbase console it only shows this message:
Function execution took 1438 ms, finished with status code: 200
Exception from a finished function: Error: Cannot read properties of undefined (reading 'firestore')
I receive null in my client app:
data: null
I googled the error message but no one seemed to have posted something like this anywhere.
I have tried placing my code inside try catch but it seems to not catch the error or whatever is happening there. I am stuck at this point, dont really know what else to do in order for me to understand what's going on. My biggest bet is that the geotransaction.get()
is not accepting the collection reference.
below is how I include the needed modules:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const firestore_ = admin.firestore();
const GeoPoint = admin.firestore.GeoPoint;
const FieldValue = admin.firestore.FieldValue;
const _geofirestore_ = require("geofirestore");
_geofirestore_.initializeApp(firestore_);
and below is my function code:
const longitude = data.longitude;
const latitude = data.latitude;
const thirty_mins_old = data.thirty_mins_old;
const currenttime = data.currenttime;
try {
const GeoFirestore_ = new _geofirestore_.GeoFirestore(firestore_);
const sfDocRef = GeoFirestore_.collection('chats')
.limit(1)
.near({ center: new GeoPoint(latitude, longitude), radius: 1 })
.native.where("created", ">=", thirty_mins_old)
.orderBy("created", "desc");
GeoFirestore_.runTransaction((transaction) => {
const geotransaction = new _geofirestore_.GeoTransaction(transaction);
return geotransaction.get(sfDocRef)
.then((sfDoc) => {
if (!sfDoc.exists) {
return 'doesnt exists';
} else {
return 'exists';
}
}).catch((error) => {
throw new functions.https.HttpsError('internal', error.message);
});
});
} catch (error) {
throw new functions.https.HttpsError('internal', error.message);
}
} catch (error) {
if (error.type === 'UnauthenticatedError') {
throw new functions.https.HttpsError('unauthenticated', error.message);
} else if (error.type === 'NotAnAdminError' || error.type === 'InvalidRoleError') {
throw new functions.https.HttpsError('failed-precondition', error.message);
} else {
throw new functions.https.HttpsError('internal', error.message);
}
}
CodePudding user response:
500 is a http error response to a request: 500 Internal Server Error
This error use to have many causes thus becoming hard to find why it happened, you can debug and analyse the debugging.
Although, the http status code 200 shown in your firebase console means success:
CodePudding user response:
I don’t know if this would help or not but have you tried importing the admin sdk like: import * as admin from "firebase-admin";
?