Reputation: 1233
I am developing a mobile app with React Native.
I create a serverless function which I deploy on Firebase to find a place nearby with Google Maps API.
I can get the data if I insert manually the Google Key in the code but if I use either google secret manager or env variable doesn't work.
I console log the key both using env and google secret manager and I can see the value.
But seems that is not available to the code when it does the request to the API.
I use @googlemaps/google-maps-services-js
module to do such request.
This is my function:
// index.js
const { onRequest } = require("firebase-functions/v2/https");
const { placesRequest } = require("./places");
const { Client } = require("@googlemaps/google-maps-services-js");
const client = new Client({});
exports.placesRequest = onRequest((request, response) => {
placesRequest(request, response, client);
});
// places.js
const { defineSecret } = require("firebase-functions/params");
const googleApiKey = defineSecret("GOOGLE_API_KEY");
const { mocks, addMockImages } = require("./mock");
const url = require("url");
const functions = require("firebase-functions");
module.exports.placesRequest = (req, res, client) => {
const { location, mock } = url.parse(req.url, true).query;
if (mock === "true") {
const data = mocks[location];
if (data) {
data.results = data.results.map(addMockImages);
}
res.json(data);
}
functions.logger.log("Logging googleApiKey from secret:", googleApiKey);
functions.logger.log(
"Logging googleApiKey from env:",
process.env.GOOGLE_API_KEY
);
client
.placesNearby({
params: {
location,
radius: 1500, //15km - is set in meter
key: process.env.GOOGLE_API_KEY, // or googleApiKey
},
timeout: 1000,
})
.then((response) => {
console.log("place response", response.data);
response.data.results = response.data.results.map(addGoogleImage);
return res.json(response.data);
})
.catch((err) => {
res.status(400);
return res.send(err);
});
};
Any tips? Would be highly appreciate
Upvotes: 0
Views: 262
Reputation: 1233
I manage to fix it. I simply created another variable in the env file and give different name and same value. No idea what was the problem... maybe the name I gave is already taken from Google space, or maybe there was some invisible space or chars. Another lessons how starting from scratch when I am stuck is a good idea ;)
Google Secret manager still doesn't work, but I did not put the work to discover why.
Upvotes: 0