简体   繁体   English

如果javascript中出现循环,如何处理firebase ref?

[英]How to treat a firebase ref in case of loops in javascript?

So I am working on a loop and I am wondering whats the recommended way to deal with situations where you are looping through a bunch of firebase storage items and you are going to use bunch of firebase child refs.所以我正在研究一个循环,我想知道什么是推荐的方法来处理你循环访问一堆 firebase 存储项目并且你将使用一堆 firebase child refs 的情况。 I am using Node.js 12.18.2 and I am writing function for firebase cloud functions and I am going to loop through a bunch of files stored inside firebase storage and I wanna keep variable hoisting in mind while writing the code.我正在使用 Node.js 12.18.2,我正在为 firebase 云函数编写函数,我将遍历存储在 firebase 存储中的一堆文件,我想在编写代码时记住变量提升。

//should I put the ref here? and ofc, which one is better to use var or let?
let storageRef = firebase.storage();
var storageRef = firebase.storage();

item.forEach(element => {
// or use seperate storage ref for each firebase storage item
let storageRef = firebase.storage();
var storageRef = firebase.storage();

let storageRef.child("images/stars.jpg").getDownloadURL(); //bla bla bla
});

basically don't want the storage ref to somehow collide with each other.基本上不希望存储引用以某种方式相互碰撞。 what's the best option?什么是最好的选择?

firebase.storage() returns the same singleton object that will not change over the lifetime of the initialized firebase object. firebase.storage()返回相同的单例对象,该对象在初始化的firebase对象的生命周期内不会改变。 It costs essentially nothing to access it.访问它基本上不需要任何费用。 If you want to reuse that object, it makes more sense to use const to declare it at the top level of your program so your code makes it clear that the variable will never change after it's assigned.如果您想重用该对象,在程序的顶层使用const声明它更有意义,这样您的代码就会清楚地表明该变量在分配后永远不会改变。

Also I would not call it storageRef , since a Reference is a special type of object in Cloud Storage (which you get when you call child()), which I wouldn't want to confuse with this singleton object.此外,我不会将其storageRef ,因为 Reference 是 Cloud Storage 中的一种特殊类型的对象(您在调用 child() 时会得到它),我不想将其与这个单例对象混淆。

const storage = firebase.storage();

Lastly, it looks like you're using the JavaScript client SDK in Cloud Functions, which could be problematic.最后,您似乎在 Cloud Functions 中使用 JavaScript 客户端 SDK,这可能会出现问题。 You should consider using the nodejs SDK instead, which will be easier to work with in Cloud Functions, and also give you full admin access to your storage bucket.您应该考虑改用nodejs SDK ,这将更容易在 Cloud Functions 中使用,并且还为您提供对存储桶的完全管理员访问权限。 If you want to create a download URL, you should use a different strategy .如果要创建下载 URL,则应使用不同的策略

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM