我有这个代码来为图像ref和产品创建常量
export const createProduct = async (productData, image) => {
const imageTimestamp = Date.now().toString();
const newImageRef = await firebaseStorage
.ref(`/images/${imageTimestamp}`).toString();
const newProductRef = await firebaseDb.ref("products");
const uploadTask = await firebaseStorage
.refFromURL(newImageRef)
.put(image)
if (uploadTask.state === "success") {
const url = await firebaseStorage
.ref(newImageRef)
.child(imageTimestamp)
.getDownloadURL()
const result = {
...productData,
image: url,
};
const postRef = firebaseDb.ref(newProductRef).push();
return postRef
.set(result)
.then((product) => {
return {
product,
status: "ok"
};
})
.catch(() => ({ status: "error" }));
}
return { status: "failure" };
};
但是我得到了这个错误
Firebase存储:ind..子路径的
ref
中的参数无效,但得到URL,请改用refFromURL。
如果我有错,请帮助我
从错误消息中可以看出,您正在将下载URL传递到对child(...)
的调用中。child()
方法只能用于相对路径,如child("dir")
或child("image.jpg")
。如果您有完整的下载URL,请使用firebaseStorage.refFromURL(...)
不过,在第二个例子中,您似乎在用StorageReference
实例做一些奇怪的事情。这应该更接近:
const imageTimestamp = Date.now().toString();
const newImageRef = await firebaseStorage.ref(`/images/${imageTimestamp}`);
const newProductRef = await firebaseDb.ref("products");
const uploadTask = await newImageRef.put(image)
if (uploadTask.state === "success") {
const url = await newImageRef.getDownloadURL()
...