У меня есть сценарий использования, когда мне нужно использовать библиотеку geofirex внутри облачных функций Google.Функция init библиотеки ожидает экземпляр FirebaseApp.Можно ли инициализировать его внутри облачных функций для запроса базы данных firestore с помощью firebase-admin?
Что я пытался ..
functions> src> index.ts
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
admin.firestore().settings({timestampsInSnapshots: true});
// Init GeoFireX
import * as firebase from 'firebase/app';
import * as geofirex from 'geofirex';
const geoFire = geofirex.init(firebase);
exports.geofenceUserNotifications = functions.https.onRequest((req, res) => {
if(!req || !req.body || !req.body.length || !req.body[0].latitude || !req.body[0].longitude || !req.body[0].uid || !req.body[0].userFcmToken){
// end function with 401 status
res.status(401).send('Authentication required.');
}
const uid = req.body[0].uid;
const userFcmToken = req.body[0].userFcmToken;
const latitude = req.body[0].latitude;
const longitude = req.body[0].longitude;
const field = 'position';
geoFire.collection('users').setPoint(uid, field, latitude, longitude).then(result => {
res.end('User geoPoint updated!');
}).catch( err => {
console.log(err);
res.end('error when update user geo point!');
});
});
functions> package.json
{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "~6.0.0",
"firebase-functions": "^2.1.0",
"moment": "^2.23.0",
"firebase": "^5.10.1",
"geofirex": "0.0.6",
"rxjs": "^6.3.3",
"rxjs-compat": "^6.5.1"
},
"devDependencies": {
"tslint": "~5.8.0",
"typescript": "~2.8.3"
},
"private": true
}
Нет ошибки при развертывании, но при вызове функции geofenceUserNotifications я получаю эту ошибку:
TypeError: app.firestore is not a function
at new GeoFireCollectionRef (/user_code/node_modules/geofirex/dist/index.cjs.js:1465:24)
at GeoFireClient.collection (/user_code/node_modules/geofirex/dist/index.cjs.js:1639:16)
at exports.geofenceUserNotifications.functions.https.onRequest (/user_code/lib/index.js:2197:13)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/providers/https.js:57:9)
at /var/tmp/worker/worker.js:783:7
at /var/tmp/worker/worker.js:766:11
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
Заранее спасибо.