【发布时间】:2019-08-04 16:14:53
【问题描述】:
我在使用 Firebase 存储时遇到了一些问题, 我正在做注册功能以与实时 Firebase 集成并保存一些数据,如“用户名、头像等。 我使用反应原生图像选择器来选择图像并将响应作为对象发送到数据库现在我需要获取此图像并将它们保存到 Firebase 存储这是我的代码
注册
import React from "react";
import { Text, TextInput, View, TouchableOpacity, Image } from "react-native";
import styles from "../Style/styles";
import firebase from "react-native-firebase";
import ImagePicker from "react-native-image-picker";
const options = {
title: "Select Avatar",
storageOptions: {
skipBackup: true,
path: "images"
}
};
export default class signUp extends React.Component {
constructor(props) {
super(props);
this.state = {
email: "",
password: "",
username: "",
avatarSource: "",
errorMessage: null
};
}
SignUpUser = (
email,
password,
username,
mobileNumber,
city,
avatarSource
) => {
try {
// if (this.state.password.length < 6) {
// alert("Please Enter At least 6 characters");
// return;
// }
// var userId = firebaseApp.auth().currentUser.uid;
firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.then(user => {
firebase
.database()
.ref(`users/${user.uid}`)
.set({
email: email,
username: username,
mobileNumber: mobileNumber,
city: city
avatarSource: avatarSource
});
// console.log("uid:", user);
firebase.storage().ref(`UserAvatar/${avatarSource.data}`);
this.props.navigation.navigate("Home");
});
} catch (error) {
console.log(error.toString());
}
};
handleAvatar = () => {
ImagePicker.showImagePicker(options, response => {
console.log("Response = ", response);
if (response.uri) {
this.setState({ avatarSource: response });
}
});
};
render() {
const { avatarSource } = this.state;
return (
<View style={styles.container}>
<Text style={{ color: "#e93766", fontSize: 40 }}>Sign Up</Text>
{this.state.errorMessage && (
<Text style={{ color: "red" }}>{this.state.errorMessage}</Text>
)}
<TouchableOpacity onPress={this.handleAvatar}>
<Image
source={{ uri: avatarSource.uri }}
style={[styles.uploadAvatar, { borderRadius: 100 }]}
resizeMode="cover"
/>
</TouchableOpacity>
<TextInput
placeholder="Username"
autoCapitalize="none"
style={styles.textInput}
onChangeText={username => this.setState({ username })}
value={this.state.username}
/>
<TextInput
placeholder="Email"
autoCapitalize="none"
style={styles.textInput}
onChangeText={email => this.setState({ email })}
value={this.state.email}
/>
<TextInput
placeholder="City"
autoCapitalize="none"
style={styles.textInput}
onChangeText={city => this.setState({ city })}
value={this.state.city}
/>
<TextInput
placeholder="Mobile Number"
keyboardType="numeric"
style={styles.textInput}
onChangeText={mobileNumber => this.setState({ mobileNumber })}
value={this.state.mobileNumber}
maxLength={10}
/>
<TextInput
secureTextEntry
placeholder="Password"
autoCapitalize="none"
style={styles.textInput}
onChangeText={password => this.setState({ password })}
value={this.state.password}
/>
<TouchableOpacity
onPress={() =>
this.SignUpUser(
this.state.email,
this.state.password,
this.state.username,
this.state.mobileNumber,
this.state.city,
this.state.avatarSource
)
}
>
<Text style={{ color: "#e93766", fontSize: 18 }}>Sign Up </Text>
</TouchableOpacity>
<View>
<Text>
Already have an account?
<Text
onPress={() => this.props.navigation.navigate("Login")}
style={{ color: "#e93766", fontSize: 18 }}
>
Login
</Text>
</Text>
</View>
</View>
);
}
}
【问题讨论】:
-
我觉得你的问题不是很清楚,能不能说的更准确点?
-
嗯,我想从我的手机上传一张图片到我们的 Firebase 存储,好吗?但是现在我将它们保存到实时数据库中,正如您在屏幕截图中看到的那样,我需要获取此图像并将它们保存到 firebase 存储
-
为什么必须将它从实时数据库移动到存储?不能直接上传到存储吗?
-
Idk :D,我写了一个函数来注册和保存自定义数据,并将头像包含到实时数据库中,当我按下注册按钮时,我调用了这个函数一次,所以!
标签: reactjs firebase react-native firebase-realtime-database firebase-storage