Firebase存储添加文件引用到firestore

Lan*_*usT 4 javascript firebase firebase-storage google-cloud-firestore

我正在尝试让表单上传工作,以将数据上传到我的 Firestore 数据库以及将图像上传到我的 Firebase 存储。

我个人可以同时执行这两项操作,但是除了存储上传图像的确切 URL 之外,我似乎无法弄清楚如何以编程方式将图像引用存储在我的 firestore 中。

在控制台中,我可以将类型设置为“参考” 在此输入图像描述

但无论我以编程方式尝试什么都不起作用:

帖子图片上传:

const url = await storageRef.snapshot.ref.getDownloadURL()
let imageRef = storage.ref().child(`test/${this.imageData.filename}`).ref
// let imageRef = storageRef.snapshot.ref
// let imageRef = storage.ref(`test/${this.imageData.name}`)

const docRef = await testImagesCollection.add({
   thumbnail: imageRef,
   dateCreated: firestore.FieldValue.serverTimestamp()
})

alert("upload succeeded", docRef.id)

Run Code Online (Sandbox Code Playgroud)

我通常会得到:FirebaseError: Function DocumentReference.set() called with invalid data. Unsupported field value: a custom Reference object (found in field thumbnail)

如果必须的话,我只会存储 URL,但我宁愿不这样做,如果控制台允许我设置引用,我也应该能够以编程方式完成它?!

Dou*_*son 6

Firestore 引用类型仅适用于对表示为DocumentReference对象的其他文档的引用。它们不适用于对 Cloud Storage 中对象的引用。如果您想要在 Cloud Storage 中存储对某个对象的引用,您应该将文件的路径存储在存储桶中(作为字符串,而不是作为引用对象),或者下载 URL(以对您来说更方便的方式) 。

如果您有一个Reference对象,则可以使用其fullPath属性获取它的字符串路径。