Firebase云函数用于获取文档。

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

Firebase Cloud Function to fetch documentd

问题

我正在制作一个iOS应用程序,在其中我使用云函数来执行应用程序的所有后端任务。

但我有一个问题...

如果我实现一些云函数来获取文档,而不是直接从应用程序中获取,这是否是一个好主意,对我有好处呢?

这只是为了获取数据并进行一些逻辑操作,然后将快照数据返回给用户。

类似于这样:

exports.getLikedComments = functions.https.onCall(async (data, {auth}) => {
  const uid = auth?.uid;
  if (uid == null) return {alert: "access-denied"};

  try {
    const id = data.post_id;

    return await db.collection("posts").doc(id).collection("liked-comments").doc(uid).get();
  } catch (err) {
    functions.logger.log(err);
    throw err;
  }
});
英文:

I am making an ios app where I use the cloud functions to do all the back-end tasks of the app.

But I had a question...

It's a good idea and it would benefit me if I implement some cloud function that fetch documents, instead of doing it directly from the app.

It would just be to get the data and do a bit of logic, to return the snapshot data to the user.

Something like this:

exports.getLikedComments = functions.https.onCall(async (data, {auth}) => {
  const uid = auth?.uid;
  if (uid == null) return {alert: "access-denied"};

  try {
    const id = data.post_id;

    return await db.collection("posts").doc(id).collection("liked-comments").doc(uid).get();
  } catch (err) {
    functions.logger.log(err);
    throw err;
  }
});

答案1

得分: 0

对于您分享的代码,在 Cloud Functions 中运行此代码与在您的应用程序中运行等效代码并将安全规则设置为允许每个经过身份验证的用户读取访问权限之间几乎没有什么区别。

这是否是一个好主意,很大程度上取决于您对好主意的定义。由于这是主观的,我们无法在 Stack Overflow 上客观地回答这个问题。您(作为新手使用无服务器技术)可能会觉得从您的应用程序直接访问数据很奇怪,而我(作为后端服务专家)想知道您为什么要这样使用云函数。这两种观点都不是完全错误的,它们只是基于我们个人经验的主观看法。

英文:

For the code you shared there is very little difference between running this code in Cloud Functions or running the equivalent code in your app itself with the security rules set to allow every authenticated user read access.

Whether that is a good idea, depends largely on your definition of a good idea. Since that is subjective, it is not something we can objectively answer here on Stack Overflow. You (being new to serverless) may find it weird to access data directly from your app, while I (a BaaS veteran) wonder why on earth you'd use a Cloud Function like that. Neither of those viewpoints is pertinently wrong, they're just subjective - based on our personal experiences.

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

发表评论

匿名网友

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

确定