我正在做我的Firebase项目,直到昨天为止一切都很好-已经一个月左右了。我不知道为什么 秘密 停止工作。
简而言之,我下面的名为 uploadProfilePic 的函数用于从Firebase Storage中返回Promise。// upload [profilePic] to [destPath]
export const uploadProfilePic = (destPath, profilePic, isCropped) => dispatch => {
dispatch(requestUploadPicToStorage(destPath));
let ref = storageRef.child(destPath);
// create file metadata including the content type
var metadata = {
contentType: profilePic.fileType,
};
// upload the data_url string to Firebase's Storage
if (isCropped)
return ref.putString(profilePic.cropURL, 'data_url', metadata); // promise returned
else
return ref.putString(profilePic.oriURL, 'data_url', metadata); // promise returned
}
我添加了一个 Promise.all()来检查是否没有兑现任何承诺。它在过去的一个月中一直有效,并于昨天停止工作。我不知道为什么它突然停止工作。
export const uploadOriAndCrop = (username, profilePic, isOriPicUpdated) => dispatch => {
let croppedPath = "/users/" + username + "_cropped_profile_pic" + profilePic.fileType;
let oriPath = "/users/" + username + "_profile_pic" + profilePic.fileType;
const promises = [];
// upload cropped pic to firebase storage
const promise1 = dispatch(uploadProfilePic(croppedPath, profilePic, true));
promises.push(promise1); // promise1 is undefined
// if we need to update the original picture
if (isOriPicUpdated) {
// upload original pic to firebase storage
const promise2 = dispatch(uploadProfilePic(oriPath, profilePic, false));
promises.push(promise2); // promise2 is undefined
}
// [promise1, promise2] is "undefined"
return Promise.all(promises);
}
让我困惑的是,如果我这样称呼它,它会完美工作:
...
ref.putString(profilePic.cropURL, 'data_url', metadata)
.then((snapshot) => {
console.log(snapshot);
})
...
我可以通过重复几次第三段代码轻松地逃脱。但是,我希望代码简洁明了。
谁能告诉我 uploadProfilePic 在 uploadOriAndCrop 中调用返回的“未定义”值的可能原因是什么?
谢谢!
其他信息
ref.putString(profilePic.cropURL, 'data_url', metadata);
确实返回一个值,并且不是未定义的。
// upload the data_url string to Firebase's Storage
if (isCropped)
{
const val = ref.putString(profilePic.cropURL, 'data_url', metadata);
console.log(val);
return val; // val is not undefined
}
else
{
const val = ref.putString(profilePic.oriURL, 'data_url', metadata);
console.log(val); // val is not undefined
return val;
}