Firebase可调用云函数生成的动态链接

huangapple go评论64阅读模式
英文:

Firebase Dynamic Links from Callable Cloud Function

问题

我正在尝试将我的iOS应用的整个后端放入云函数中,以提高应用的安全性。

但是我在尝试从云函数生成动态链接时遇到了困难。

我是根据以下教程进行操作的:
https://medium.com/the-andela-way/creating-firebase-dynamic-links-with-firebase-cloud-functions-e96d1713530f

有人可以告诉我我漏掉了什么,或者为什么我会收到以下错误:

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)

函数:

import * as functions from "firebase-functions";
import urlBuilder = require("build-url");
import request = require("request-promise");

// HTTP_Callable_Dynamic_Links_Service

// 创建空间邀请
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,
    },
  });
}

希望这能帮助你解决问题。如果你需要更多帮助,请提供更多详细信息。

英文:

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,
},
});
}

答案1

得分: 0

错误可能出现在这一行:

uri: `https://[redacted]?key=${functions.config().applinks.key}`,

functions.config().applinks 未定义。这就是错误尝试告诉你的内容。在部署之前,你需要按照教程中的步骤设置此函数的配置,这意味着你可能在教程中漏掉了一步。请查看标题为“设置函数环境配置”的部分。

英文:

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".

huangapple
  • 本文由 发表于 2023年6月29日 04:33:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定