Javascript Firebase存储任务(进度条)
我正在尝试使用Firebase storage执行内容上载进度条,但从函数返回任务时遇到一些问题 我已经使用React上下文API实现了Firebase单例。在Firebase组件中,我有多个函数,其中一个称为“uploadContent” 代码如下:Javascript Firebase存储任务(进度条),javascript,reactjs,firebase,react-native,firebase-storage,Javascript,Reactjs,Firebase,React Native,Firebase Storage,我正在尝试使用Firebase storage执行内容上载进度条,但从函数返回任务时遇到一些问题 我已经使用React上下文API实现了Firebase单例。在Firebase组件中,我有多个函数,其中一个称为“uploadContent” 代码如下: uploadContent = async (postInfo) => { const { uri, description, location, tags } = postInfo; // Post UUID
uploadContent = async (postInfo) => {
const { uri, description, location, tags } = postInfo;
// Post UUID
const postId = uuid();
// Upload to firestore
const data = {
id: postId,
description,
location,
tags,
time: firestore.Timestamp.fromDate(new Date()), // The time when the image is uploaded
likes: [], // At the first time, when a post is created, zero users has liked it
comments: [], // Also, there aren't any comments
};
await this.db
.collection("posts")
.doc(this.auth.currentUser.uid)
.collection("userPosts")
.add(data);
// Create a storage referece
const storageRef = firebase.storage().ref("photos").child(postId);
// Uri to Blob
const response = await fetch(uri);
const blob = await response.blob();
// Upload to storage
const task = storageRef.put(blob);
return task;
};
问题是,当我从uploader组件调用这个函数,并尝试使用其中一个返回的对象函数时,我得到“[Unhandled promise rejection:TypeError:undefined不是一个函数(在“…task.on…”附近)”,我不知道如何解决这个问题
Pd:如果我在“uploadContent”方法(我创建任务的地方)内调用这个函数,它可以正常工作,但我需要返回任务
下面是我调用firebase方法的函数代码:
const upload = async () => {
const { firebase, navigation } = props;
console.log("Uploading...");
// Prepare post information
const postInfo = {
uri: photo.uri,
description: descriptionInput.current.props.value,
location: locationName, // TODO - Object with the location coords too
tags: [], // TODO - users tagged
};
// Upload to firebase
const task = await firebase.uploadContent(postInfo);
task.on("state_changed", (taskSnapshot) => {
console.log(
`${taskSnapshot.bytesTransferred} transferred out of ${taskSnapshot.totalBytes}`
);
});
// navigation.navigate("Profile"); // TODO: route params -> task
};
有什么想法吗?谢谢。不久前我也遇到了同样的问题。你找到解决办法了吗?没有。我记得当我尝试console.log任务时,我需要使用JSON.stringify,但没有打印任何内容。所以我决定使用ActivityIndicator,这不是最优雅的解决方案,但也不错。我需要进度条