简体   繁体   English

如何使用 firebase 函数返回保存在 firebase 存储中的文件的 json 内容

[英]How to use firebase functions to return the json content of a file saved in firebase storage

I want to frequently update client side indexeddb data store from the cloud.我想经常从云端更新客户端 indexeddb 数据存储。 So what I am thinking is to call the firebase function and have the function fetch an update json file in firebase storage and then return the content of that json file to client side which will update the indexeddb afterwards.所以我在想的是调用 firebase 函数并让该函数在 firebase 存储中获取更新 json 文件,然后将该 json 文件的内容返回给客户端,然后客户端将更新 indexeddb。

I tried to return this json file with this code我尝试使用此代码返回此json 文件

exports.updateProductCatalogue = functions.https.onCall(async (data, context) => {
    const filepath = data.filepath
    const bucketname = data.bucket

     
    const bucket = storage.bucket();
    const remoteFile = bucket.file("storeid.json");

    let buffer = '';
    remoteFile.createReadStream()
    .on('error', function(err) {console.log(err)})
    .on('data', function(response) {
        buffer += response
    })
    .on('end', function() {
        //console.log(buffer);
        return buffer
     
    })
  })

when ever I try to deploy the code to the cloud it throws this error当我尝试将代码部署到云时,它会抛出此错误

firebase-functions/functions/node_modules/@google-cloud/storage/node_modules/p-limit/index.js:30
    } catch {}
            ^

SyntaxError: Unexpected token {
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)

I was able to narrow the source of the error to this code我能够将错误来源缩小到此代码

 const bucket = storage.bucket();

How can I return the json content on the file via firebase functions?如何通过 firebase 函数返回文件中的 json 内容?

This is not an issue with your code (phew!) but a configuration of your project.这不是您的代码问题(呸!),而是您的项目配置。 Try using node 10+ to address the exception syntax error .尝试使用节点 10+ 来解决异常语法错误 After you update node+npm, don't forget to update you package.json :更新 node+npm 后,不要忘记更新package.json

{
"...": "...",
  "engines": {
    "node": "10",
    "npm": "6"
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Firebase函数将文件列表上传到Firebase存储 - How to upload a list of file to firebase storage from firebase functions 通过 Firebase 函数下载文件到 Firebase 存储 - Download file to Firebase Storage via Firebase Functions 如何使用 Cloud Functions 中的“onFinalize”将 300Mb JSON 文件从 Firebase 存储导入数据库? - How to import 300Mb JSON file from Firebase Storage to Database using 'onFinalize' in Cloud Functions? Firebase:云函数+存储读取文件 - Firebase: Cloud Functions + Storage Read File Firebase存储和云功能:如何从url加载内容并将其保存到存储中? - Firebase Storage and Cloud functions: how to load content from url and save into storage? 使用 Firebase Cloud Functions 将文件上传到 Firebase 存储时出错 - Error while uploading file to Firebase Storage using Firebase Cloud Functions 上传到Firebase存储时如何使用Cloud Functions for FIrebase获取mp3文件的持续时间 - How to get the duration of a mp3 file using Cloud Functions for FIrebase when uploaded to firebase storage Firebase Cloud Functions - 如何在 multiple.then() 方法中使用相同的返回值 - Firebase Cloud Functions - How to use same return value in multiple .then() methods 如何使用Firebase云功能Node.js访问Firebase存储 - How to access Firebase storage with Firebase cloud functions Node.js 如何使用Firebase功能处理抓取的图像并上传到Firebase存储? - How to process a scraped image and upload to firebase storage using firebase functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM