[英]Dynamic naming of .update key Firebase
I have a form where I am uploading many images to different locations, grabbing the downloadURL
and using ref.update
to store the URL of the image in the correct DB ref.我有一个表单,我将许多图像上传到不同的位置,获取
downloadURL
并使用ref.update
将图像的 URL 存储在正确的数据库引用中。
I'm wondering if it's possible to use a variable in .update
to be able to change the key dynamically by grabbing the name of the active file input:我想知道是否可以在
.update
使用变量来通过获取活动文件输入的名称来动态更改密钥:
handleImgUpload() {
var activeInput = document.activeElement.name;
// set up storage refs, upload file then...
imgStorageRef.child(filename).getDownloadURL().then(function(url){
ref.update( {
activeInput: url // HERE IS WHERE I WANT THE VALUE STORED IN activeInput
})
In this example the ref
to be updated will get the string 'activeInput' as the key.在这个例子中,要更新的
ref
将获得字符串 'activeInput' 作为键。 Is there a way to add the value that was stored in var activeInput
instead?有没有办法添加存储在
var activeInput
的值?
It sounds like you're trying to dynamically set the property of an object.听起来您正在尝试动态设置对象的属性。 To do so use
[]
notation.为此,请使用
[]
表示法。 So:所以:
handleImgUpload() {
var activeInput = document.activeElement.name;
// set up storage refs, upload file then...
imgStorageRef.child(filename).getDownloadURL().then(function(url){
var updates = {};
var key = "activeInput";
updates[activeInput] = url;
ref.update(updates)
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.