在响应功能中呼叫等待



我正在尝试在 response => {}中使用等待的调用但是,我在等待的期待中遇到了一个错误,上面写着新线或半龙。我认为错误是由于功能的结构造成的,但我不确定如何纠正它。

我要做的是添加/更新用户的配置文件映像。当用户单击触摸可接触性缩略图时,将调用_pickImageHandler(),如果选择图像并且响应还可以,它将等待调用uploadImageToStorage功能,该功能将将图像上传到firebase存储。

如果 uploadImageToStorage返回true,我想调用editProfileimage将下载图添加到用户文档中。

_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

相关内容

  • 没有找到相关文章

最新更新