在本机反应中将图像上传到firebase:undefined不是函数
Uploading image to firebase in react native: undefined is not a function
正如标题所说,我正在尝试将图像上传到本机反应中的 firebase。为此,我正在使用 react-native-image-picker
和 firebase
模块。我的代码如下:(为清楚起见,仅包括 "main" 部分)
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
函数。请注意,我已经提供了所需的权限。因此,这不是问题。请帮我解决这个问题。谢谢...
您在使用 React-native
ImagePicker
吗? API 文档中没有 open
。
API Reference 个 react-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,
});
}
});
正如标题所说,我正在尝试将图像上传到本机反应中的 firebase。为此,我正在使用 react-native-image-picker
和 firebase
模块。我的代码如下:(为清楚起见,仅包括 "main" 部分)
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
函数。请注意,我已经提供了所需的权限。因此,这不是问题。请帮我解决这个问题。谢谢...
您在使用 React-native
ImagePicker
吗? API 文档中没有 open
。
API Reference 个 react-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,
});
}
});