firebase - 清除所有站点数据(或应用程序的新用户)时,Firebase 存储上传不起作用
问题描述
这是我过去几周一直试图解决的问题,但没有成功。我在我的项目中使用https://firebase.nuxtjs.org/ 。我的商店操作运行良好,当网站存储一些数据时,图像已成功上传,这意味着如果您不是新用户并且您尚未从 chrome>settings>site settings 中清除站点数据。但是,如果您这样做,则相同的代码将不起作用,并且只会上传一张图片,有时甚至不会上传一张图片。
最糟糕的是,firebase 上传任务和 catch 块都没有抛出错误。清除所有站点数据时,不调用上传任务的第三个回调,表示上传不成功。也是第一个回调,使用快照参数仅调用一次,并记录 0% 的上传进度。再次尝试时(例如,在将某些数据存储为 cookie 之后)调用相同的上传回调并查看日志。
这是我使用的代码。我知道这很丑陋,但是在我尝试了一切之后,我只想找到问题然后重构。请帮忙。
createAd ({ commit }, ad) {
this.$fire.database.ref('ads').push(ad).then(data => data.key)
.then(async (key) => {
console.log('adding new ad with unique key:', key);
const images = []
this.$fire.database.ref('ads').child(key).update({ id: key })
const storageRef = this.$fire.storage.ref();
const imageRef = storageRef.child(`ad_images/${key}${ad.mainImage.name}`);
// This is not resolved if you clear site data
const mainImageUpload = imageRef.put(ad.mainImage);
mainImageUpload.on('state_changed',
(snapshot) => {
const uploadPercentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.error('upload progress in %:', uploadPercentage);
},
error => console.error(error),
() => {
console.error('main image upload succesful');
mainImageUpload.snapshot.ref.getDownloadURL().then((downloadURL) => {
console.error('main image uploaded at:', downloadURL);
mainImageUpload.snapshot.ref.getMetadata().then(({ name }) => {
ad.mainImageUrl = { data: downloadURL, name };
console.log('adding main image url for ad', ad.mainImageUrl);
this.$fire.database.ref('ads').child(key).update({ mainImageUrl: { data: downloadURL, name } })
if (!ad.images.length) {
commit('createAd', { id: key, ...ad })
}
});
});
}
);
const uploadImage = (image, i) => {
return new Promise((resolve) => {
const storageReference = this.$fire.storage.ref();
const imageReference = storageReference.child(`ad_images/${key}${image.name}`);
const imageUpload = imageReference.put(image);
imageUpload.on('state_changed',
(snapshot) => {
const uploadPercentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
console.error('image upload progress in %:', uploadPercentage);
},
error => console.error(error),
() => {
console.log('successful image upload');
imageUpload.snapshot.ref.getDownloadURL().then((downloadURL) => {
console.error('image uploaded at:', downloadURL);
imageUpload.snapshot.ref.getMetadata().then(({ name }) => {
console.log('adding images for ad', name, downloadURL)
images.push({ data: downloadURL, name })
if (i === ad.images.length - 1) {
this.$fire.database.ref('ads').child(key).update({ imageUrls: images })
ad.imageUrls = images;
commit('createAd', { id: key, ...ad });
}
return resolve();
});
});
});
})
}
if (ad.images.length) {
for (let i = 0; i < ad.images.length; i++) {
const image = ad.images[i];
await uploadImage(image, i);
}
}
}).catch(error => console.error('creating ad error', error));`
解决方案
推荐阅读
- python - 我需要为此获得连续输入,但是当我使用 while 循环时,它会中断
- c++ - 限制数组类型的大小,虽然还没有实例
- bash - 我怎样才能得到 whoami 因为它会存在 *before* sudo 到变量中?
- datastage - 修剪前导和尾随空格而不使用数据阶段中的转换阶段
- python - Saleor电子商务:如何通过python代码或API批量插入产品
- insert - 使用 JpaRepository 进行插入并检查它是否成功的最佳方法是什么?
- pandas - 有没有办法在 Wing Personal 7.0 中很好地查看数据帧?
- c - 功能正在打印我的字符远离它应该在的位置
- c# - EF Core - 唯一约束,包括迁移中未检测到的导航属性
- bash - 从 Notepadqq 运行 bash 脚本不起作用