【问题标题】:How to read a JSON file data and use it in firebase cloud function如何读取 JSON 文件数据并将其用于 Firebase 云功能
【发布时间】:2018-12-08 19:31:10
【问题描述】:

我有一个 firebase 云函数,它将在 HTTP 请求上被调用,它工作正常。

现在,我想从 JSON 文件中读取一些业务逻辑的数据。以下是我尝试读取 JSON 文件的两种方式:

选项 #1) 将 JSON 文件保存在我的 nodejs 项目的“公共”目录中并进行部署。得到了我正在使用的托管 URL,如下所示。但它抛出一个错误说'错误:getaddrinfo ENOTFOUND ...'

选项 #2) 将 JSON 文件上传到 Firebase 云存储。没有找到任何例子来试试这个。最终得到以下代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Firestore = require('@google-cloud/firestore');
const firestore = new Firestore();
const http = require('http');
const url = require('url');

// Option #2 required variables
var storage = require('@google-cloud/storage');
const gcs = storage({projectId: "<Project ID>"});
const bucket = gcs.bucket("<bucket-name>");
const file = bucket.file("<filename.json>")

// HTTP Trigger
exports.functionName = functions.https.onRequest((req, res) => {
 var request = require('request'); 
 var paramValue = req.body.queryParam;
 console.log(paramValue);

// Option #1 - Using hosted URL
var hostingURL = "https://xxxxxxxx.firebaseapp.com/filename.json";
 console.log(hostingURL);
 request({
        url:hostingURL,
        method: 'POST', 
        json:{ key: 'value' } },function(error, response, data) {
 });
  
// Option #2 - Ended up here. Want to read from cloud storage bucket.
console.log(file);

});

有人可以帮我吗?

【问题讨论】:

  • 请编辑您的问题,详细说明您的功能应该如何工作。你真正想说的是你尝试了一些东西,但这些东西并没有按照你期望的方式工作。除了“读取 JSON 文件”之外,您究竟需要完成什么,这有点模糊。
  • 我的云函数是一个 HTTP 触发器,可以调用为“us-central1-xxxxx.cloudfunctions.net/… ”。我将“paramValue”值作为“request.query.queryParam”。现在,如果 JSON 文件中存在“paramValue”,我需要返回成功。为了将“paramValue”与 JSON 文件中的数据进行比较,我需要读取云函数中的 JSON 文件。如果您需要更多详细信息,请告诉我。
  • 谢谢@DougStevenson。由于我想使用免费计划并将其演示到业务中,有没有办法实现选项#2?
  • 是的,有可能。

标签: node.js firebase google-cloud-storage google-cloud-functions firebase-storage


【解决方案1】:

您可以像@AlexM 所说的那样使用require('firebase-admin'),也可以使用客户端包:@google-cloud/storage

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const fileBucket = 'your-bucket.appspot.com';
const filePath = 'your-json.json';
const bucket = storage.bucket(fileBucket);
const file = bucket.file(filePath);
file.download()
  .then((data) => {
    const obj = JSON.parse(data);
});

别忘了:

  • 部署前在函数文件夹中运行npm i @google-cloud/storage
  • JSON.parse 将您的 json 作为对象的结果

【讨论】:

    【解决方案2】:

    如果您的文件在 Firebase 可以存储中,您可以使用这种方法:

    const admin = require('firebase-admin');
    admin.storage().bucket().file("yourDirForFile/yourFile.json")
        .download(function (err, contents) {
            if (!err) {
                var jsObject = JSON.parse(contents.toString('utf8'))
            }
    });
    

    你可以随意使用变量jsObject。它现在在内存中。

    【讨论】:

      【解决方案3】:

      您可以将 .json 文件放在 index.js 所在的同一文件夹中。然后您可以执行以下操作:

      const config = require('./config.json');
      console.log(config.foo);
      

      鉴于以下 config.json 文件:

      {
          "foo" : "bar"
      }
      

      【讨论】:

      • 感谢@Thomas,您的解决方案运行良好。您是否还知道如何从 Firebase 云存储桶中读取它(正如我在选项 #2 中提到的)?如果是这样,请您分享代码sn-p。
      • 你知道这是否安全吗?我正在考虑将客户端密码字符串存储在这样的 JSON 文件中,以供我的 Firebase 函数使用。如果有人想查看该文件,他们就必须闯入 Google 的服务器,对吗?
      • 其实我在这里找到了解决方案:firebase.google.com/docs/functions/config-env。谷歌的文档甚至在第一段中说它是用于 API 密钥和内容的。
      • 错误:找不到模块'./database.json'
      • 在我的情况下,“找不到模块”错误是由于使用了打字稿而不是 javascript。我通过将 json 文件从 src 目录移动到 js 文件所在的 lib 来解决这个问题。
      猜你喜欢
      • 1970-01-01
      • 2018-06-30
      • 2022-07-07
      • 2018-03-18
      • 2020-07-26
      • 1970-01-01
      • 2019-08-08
      • 2018-03-27
      • 2018-10-30
      相关资源
      最近更新 更多