I'm trying to put all the back-end of my ios app in the cloud functions to have more security within the app.
But I got stuck trying to generate a dynamic link from the cloud functions.
From this tutorial I was guided: https://medium.com/the-andela-way/creating-firebase-dynamic-links-with-firebase-cloud-functions-e96d1713530f
Can someone tell me what I'm missing or why I'm getting the following error:
TypeError: Cannot read properties of undefined (reading 'key')
at /workspace/lib/HTTP callable functions/dynamic-links-service.js:19:110
at fixedLen (/workspace/node_modules/firebase-functions/lib/v1/providers/https.js:74:41)
at /workspace/node_modules/firebase-functions/lib/common/providers/https.js:458:32
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Function:
import * as functions from "firebase-functions";
import urlBuilder = require("build-url");
import request = require("request-promise");
// HTTP_Callable_Dynamic_Links_Service
// Create space invitation
exports.HTTP_Callable_Dynamic_Links_Service_spaceInvitation = functions.https.onCall(async (data, {auth}) => {
const uid = auth?.uid;
if (uid == null) return {alert: "access-denied"};
try {
const id: string = data.id;
const header: string = data.header;
const caption: string = data.caption;
const imageUrlString: string = data.imageUrlString;
const options = {
method: "POST",
uri: `https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=${functions.config().applinks.key}`,
body: {
"longDynamicLink": makeDynamicLongLink(id, caption, header, imageUrlString),
},
json: true,
};
functions.logger.log(options);
const shortLink = request(options);
console.log(shortLink);
return shortLink;
} catch (err) {
functions.logger.log(err);
throw err;
}
});
function makeDynamicLongLink(id, caption, header, imageUrlString) {
return urlBuilder(`${functions.config().applinks.link}`, {
queryParams: {
link: "https://myspace.page.link/invitation/" + id,
st: caption,
sd: header,
si: imageUrlString,
},
});
}
The error is probably on this line:
uri: `https://[redacted]?key=${functions.config().applinks.key}`,
functions.config().applinks
is undefined. That's what the error is trying to tell you. You need to do whatever you're supposed to do to set up the config for this function before you deploy it, that means you missed a step in the tutorial. See the section titled "Setting the functions environment config".