在响应函数中调用 await
Call await within response function
我正在尝试在 response => {}
中使用 await 调用
但是我在 await 上收到一个错误,上面写着 Expecting new line or semicolon。
我认为错误是由于函数的结构造成的,但我不确定如何纠正它。
我想做的是 add/update 用户的个人资料图片。当用户点击一个 touchableOpacity 缩略图时,如果图像被选中并且响应正常,_pickImageHandler()
将被调用,
它将等待调用 uploadImageToStorage
函数,该函数会将图像上传到 Firebase 存储。
如果 uploadImageToStorage
returns true 然后我想调用 editProfileImage 将 downloadUrl 添加到用户的文档中。
_pickImageHandler = async () => {
ImagePicker.showImagePicker({title: "Select Image", maxWidth: 800, maxHeight: 600},
// I'm assuming I have to somehow adapt the below response line to fix my problem
response => {
if (response.didCancel) {
console.log("User cancelled!");
if (!response.uri) {
this.showToast('User profile image cannot be empty', 'ok', 'warning')
}
}
else if (response.error) {
//todo look at putting below line into toast message
console.log("Error", response.error);
}
else {
console.log("pickedImage URI: ", response.uri);
console.log("pickedImage FileName: ", response.fileName);
this.setState({
//pickedImageUri: response.uri,
userProfileImageUrl: response.uri,
imageName: response.fileName
});
// Im getting the error on the below line
// Expecting a new line or semicolon
let didImageUploadToStorage = await this._uploadImageToStorage(response.uri, response.fileName);
if (didImageUploadToStorage) {
console.log('didImageUploadToStorage', didImageUploadToStorage);
this.editUserProfileImage()
}
else {
console.log('didImageUploadToStorage', didImageUploadToStorage);
}
}
})
};
_uploadImageToStorage = async (uri, fileName) => {
console.log("uploadImage Triggered!");
console.log("uri: ", uri);
console.log("fileName: ", fileName);
//todo add in another folder after userEvents so it will be userEvents/eventDoc.id/filename
const storageRef = firebase.storage().ref(ref);
await storageRef.put(uri)
.then(snapshot => {
console.log("Upload Success!!!!! :) ");
console.log("downloadUrl: ", snapshot.downloadURL);
this.setState({
userProfileImageUrl: snapshot.downloadURL,
});
return true
})
.catch((error) => {
console.log('FirebaseStorage upload error:', error);
return false
});
};
editUserProfileImage = () => {
console.log('editUserProfileImage FIRED!');
console.log('editUserProfileImage text: ', this.state.userProfileImageUrl);
if (!this.state.userProfileImageUrl.length) {
this.showToast('Profile image URL cannot be empty', 'OK', 'danger');
return
}
console.log('3 userDocExists: ', this.state.userDocExists);
if (this.state.userDocExists) {
console.log('docExists == true');
this.updateFieldInDB(usersCollectionRef, 'i4Mefea0wUjHW07jEk0W', 'userProfileImageUrl', this.state.userProfileImageUrl)
}
else {
console.log('docExists == false');
this.setFieldInDB(usersCollectionRef, 'i4Mefea0wUjHW07jEk0W', 'userProfileImageUrl', this.state.userProfileImageUrl)
}
}
有没有更好更有效的方法来调用更少的函数来执行此操作。
感谢您的帮助。
将 async
添加到您的函数中,然后 await
将被识别为您函数中的有效关键字(假设您使用的浏览器支持异步函数或正在转换):
async response => {
// ...
}
要使用 await
运算符,您必须在 async
函数中使用它。将您的回调更改为类似这样的内容应该可以完成工作。
async (response) => {
// your logic
}
我看到你的代码,响应应该有 async
我正在尝试在 response => {}
中使用 await 调用
但是我在 await 上收到一个错误,上面写着 Expecting new line or semicolon。
我认为错误是由于函数的结构造成的,但我不确定如何纠正它。
我想做的是 add/update 用户的个人资料图片。当用户点击一个 touchableOpacity 缩略图时,如果图像被选中并且响应正常,_pickImageHandler()
将被调用,
它将等待调用 uploadImageToStorage
函数,该函数会将图像上传到 Firebase 存储。
如果 uploadImageToStorage
returns true 然后我想调用 editProfileImage 将 downloadUrl 添加到用户的文档中。
_pickImageHandler = async () => {
ImagePicker.showImagePicker({title: "Select Image", maxWidth: 800, maxHeight: 600},
// I'm assuming I have to somehow adapt the below response line to fix my problem
response => {
if (response.didCancel) {
console.log("User cancelled!");
if (!response.uri) {
this.showToast('User profile image cannot be empty', 'ok', 'warning')
}
}
else if (response.error) {
//todo look at putting below line into toast message
console.log("Error", response.error);
}
else {
console.log("pickedImage URI: ", response.uri);
console.log("pickedImage FileName: ", response.fileName);
this.setState({
//pickedImageUri: response.uri,
userProfileImageUrl: response.uri,
imageName: response.fileName
});
// Im getting the error on the below line
// Expecting a new line or semicolon
let didImageUploadToStorage = await this._uploadImageToStorage(response.uri, response.fileName);
if (didImageUploadToStorage) {
console.log('didImageUploadToStorage', didImageUploadToStorage);
this.editUserProfileImage()
}
else {
console.log('didImageUploadToStorage', didImageUploadToStorage);
}
}
})
};
_uploadImageToStorage = async (uri, fileName) => {
console.log("uploadImage Triggered!");
console.log("uri: ", uri);
console.log("fileName: ", fileName);
//todo add in another folder after userEvents so it will be userEvents/eventDoc.id/filename
const storageRef = firebase.storage().ref(ref);
await storageRef.put(uri)
.then(snapshot => {
console.log("Upload Success!!!!! :) ");
console.log("downloadUrl: ", snapshot.downloadURL);
this.setState({
userProfileImageUrl: snapshot.downloadURL,
});
return true
})
.catch((error) => {
console.log('FirebaseStorage upload error:', error);
return false
});
};
editUserProfileImage = () => {
console.log('editUserProfileImage FIRED!');
console.log('editUserProfileImage text: ', this.state.userProfileImageUrl);
if (!this.state.userProfileImageUrl.length) {
this.showToast('Profile image URL cannot be empty', 'OK', 'danger');
return
}
console.log('3 userDocExists: ', this.state.userDocExists);
if (this.state.userDocExists) {
console.log('docExists == true');
this.updateFieldInDB(usersCollectionRef, 'i4Mefea0wUjHW07jEk0W', 'userProfileImageUrl', this.state.userProfileImageUrl)
}
else {
console.log('docExists == false');
this.setFieldInDB(usersCollectionRef, 'i4Mefea0wUjHW07jEk0W', 'userProfileImageUrl', this.state.userProfileImageUrl)
}
}
有没有更好更有效的方法来调用更少的函数来执行此操作。 感谢您的帮助。
将 async
添加到您的函数中,然后 await
将被识别为您函数中的有效关键字(假设您使用的浏览器支持异步函数或正在转换):
async response => {
// ...
}
要使用 await
运算符,您必须在 async
函数中使用它。将您的回调更改为类似这样的内容应该可以完成工作。
async (response) => {
// your logic
}
我看到你的代码,响应应该有 async