我正在上传图片以进行头像上传。我正在使用firebase / firestore来做到这一点。 Firestore尝试上传时引发错误。有人可以建议吗?我已经尝试在此处阅读文档和其他答案,但没有运气,因此希望有经验的人可以为我提供建议,如果您能告诉我如何调试这些类型的问题以供将来参考,我将非常高兴。谢谢
我正在将AntD上传组件用于:
const uploadProps = {
action: file => this.handleUploadImage(file),
onChange: this.handleChange,
multiple: true
};
<Upload {...uploadProps} fileList={this.state.fileList}>
<Button>
<Icon type='upload' /> Upload
</Button>
</Upload>
这是我的异步操作:
export const uploadProfileImage = (file, fileName) => async (
dispatch,
getState,
{ getFirebase, getFirestore }
) => {
const imageName = cuid();
const firebase = getFirebase();
const firestore = getFirestore();
const user = firebase.auth().currentUser;
const path = `${user.uid}/user_images`;
const options = {
name: imageName
};
console.log("uploadProfileImage actions begin try");
console.log(file);
try {
// dispatch(asyncActionStart());
console.log("asyncActionStart");
// upload file to firebase storage
let uploadedFile = await firebase.uploadFile(path, file, null, options);
// get url of the image
let downloadURL = await uploadedFile.uploadTaskSnapshot.ref.getDownloadURL();
// get userdoc
let userDoc = await firestore.get(`users/${user.uid}`);
// check if user has photo, if not, update profile
if (!userDoc.data().photoURL) {
// firestore profile
await firebase.updateProfile({
photoURL: downloadURL
});
// firebase auth
await user.updateProfile({
photoURL: downloadURL
});
}
// add the image to firestore
await firestore.add(
{
collection: "users",
doc: user.uid,
subcollections: [{ collection: "photos" }]
},
{
name: imageName,
url: downloadURL
}
);
console.log("asyncActionFinished");
// dispatch(asyncActionFinish());
} catch (error) {
console.log(error);
console.log("asyncActionError");
// dispatch(asyncActionError());
}
};
console.log(文件)显示:
{file: {…}, fileList: Array(2)}
file: {uid: "rc-upload-1571170128628-2", lastModified: 1247549551674, lastModifiedDate: Tue Jul 14 2009 06:32:31 GMT+0100 (British Summer Time), name: "Penguins.jpg", size: 777835, …}fileList: (2) [{…}, {…}]__proto__: Object
firestore错误为:
FirebaseStorageError {code_: "storage/invalid-argument", message_: "Firebase Storage: Invalid argument in `put` at index 0: Expected Blob or File.", serverResponse_: null, name_: "FirebaseError"}