【问题标题】:Uploading image to firebase in react native: undefined is not a function在本机反应中将图像上传到firebase:未定义不是一个函数
【发布时间】:2019-08-03 12:45:36
【问题描述】:

正如标题所说,我正在尝试以 react native 将图像上传到 firebase。我为此使用了react-native-image-pickerfirebase 模块。我的代码如下:(为清楚起见,仅包括“主要”部分)

import ImagePicker from 'react-native-image-picker';
...
//called on pressing a button
onChooseImagePress = async () => {
    let result = await ImagePicker.open({     //error occurs here
        takePhoto: true,
        useLastPhoto: true,
        chooseFromLibrary: true
    });
    if (!result.cancelled) {
        this.uploadImage(result.uri, "test-image")
        .then(() => {
            Alert.alert("Success");
        })
        .catch((error) => {
            Alert.alert(error);
        });
    }        
}

uploadImage = async (uri, imageName) => {
    const response = await fetch(uri);
    const blob = await response.blob();

    var ref = firebase.storage().ref('images').child("userName/" + imageName);
    return ref.put(blob);
}
....

问题:

我收到此错误:undefined is not a function。这是相同的屏幕截图:

我不确定它是什么意思,因为ImagePicker 有一个open 函数。请注意,我已经提供了所需的权限。因此,这不是问题。请帮我解决这个问题。谢谢...

【问题讨论】:

  • 您是否将原生库链接到 Android 和 iOS?
  • 所以请点击此链接链接库:aboutreact.com/example-of-image-picker-in-react-native
  • @Kabir 谢谢。我会检查并告诉你
  • 使用 ImagePicker.showImagePicker(options, response => { console.log('Response = ', response); }); ImagePicker.open() 的意图

标签: android firebase react-native react-native-image-picker


【解决方案1】:

你在使用React-nativeImagePicker吗? API文档中没有open

API Referencereact-native-image-picker

这是获取你想要的所选图像的值的默认示例。

import ImagePicker from 'react-native-image-picker';

// More info on all the options is below in the API Reference... just some common use cases shown here
const options = {
  title: 'Select Avatar',
  customButtons: [{ name: 'fb', title: 'Choose Photo from Facebook' }],
  storageOptions: {
    skipBackup: true,
    path: 'images',
  },
};

/**
 * The first arg is the options object for customization (it can also be null or omitted for default options),
 * The second arg is the callback which sends object: response (more info in the API Reference)
 */
ImagePicker.launchImageLibrary(options, (response) => {
  console.log('Response = ', response);

  if (response.didCancel) {
    console.log('User cancelled image picker');
  } else if (response.error) {
    console.log('ImagePicker Error: ', response.error);
  } else if (response.customButton) {
    console.log('User tapped custom button: ', response.customButton);
  } else {
    const source = { uri: response.uri };

    // You can also display the image using data:
    // const source = { uri: 'data:image/jpeg;base64,' + response.data };

    this.setState({
      avatarSource: source,
    });
  }
});

【讨论】:

  • 它似乎工作正常(至少在控制台上打印)。反正我有一个查询。它给了我两个选择,从图书馆中选择或使用相机拍照。我不想要这些选项。要么直接带我去图书馆,要么不显示相机选项。你能帮我解决这个问题吗?
  • ImagePicker.launchImageLibrary(options, (response) => { //do something }); 这样做。为了完整起见,您能否将其添加到您的答案中?还是谢谢...
  • @AnkitKumar 我明白了。我修改了代码来回答你的问题。
猜你喜欢
  • 2018-10-30
  • 2021-10-21
  • 2020-02-22
  • 2018-11-20
  • 1970-01-01
  • 2018-05-28
  • 2022-07-18
  • 1970-01-01
  • 2020-03-23
相关资源
最近更新 更多