我正在为在线订单构建一个简单的 React Native 移动应用程序。我将所有与产品相关的信息作为字段(名称、价格、图像 URL 等)存储在 Firestore 文档中。我将图像存储在 Firebase Storage 中。当我想创建图像参考时,我使用
const imageRef = storage().refFromURL('gs://testapp-1x2y3z.firebase.com/pics/TestImage.jpg');
该程序也可以使用静态字符串值正常工作。但是,当我想从 Firestore 文档字段读取 ImageURL 时,例如
const assortmentRef = firestore().collection('Test_Store').doc('Assortment');,
imageRef = storage().refFromURL(assortmentRef.ImageURL);
即使
的字符串值也会触发错误assortmentRef.ImageURL
的值正好是
gs://testapp-1x2y3z.firebase.com/pics/TestImage.jpg I
假设问题是因为反应式原生具有异步操作。有人有解决方案吗?我的目标是输入可以从文档字段而不是静态 URL 派生的动态值。错误信息:
Possible Unhandled Promise Rejection (id: 45):
Error: firebase.storage().refFromURL(*) 'url' must be a string value and begin with 'gs://' or 'https://'.
问候, 鲍勃
是的,我尝试解析单引号和双引号内的 ImageURL 的值,但它根本不起作用。如果有任何解决方案的建议,我将非常感激。
如果我正确理解您的问题,您需要获取 Firestore 文档。
使用 const
assortmentRef = firestore().collection('Test_Store').doc('Assortment');
你只定义了一个 DocumentReference
您需要调用此 get()
的
DocumentReference
方法,如 文档中所述:遵循以下几行(使用 async
/await
语法,而文档显示 then()
语法)
const assortmentRef = firestore().collection('Test_Store').doc('Assortment');
const docSnap = await assortmentRef.get()
if (docSnap.exists) {
console.log("Document data:", doc.data());
const imageURL = doc.data().ImageURL;
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}