[英]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.