Is it possible to upload image to a user in your database and not storage. I am currently building out a registration and allowing the ability to add images to a profile and store those images in your UID. Currently all the data saves, expect the image or image url. Any thoughts of how to approach or if this is the correct way to go about this?
My JSON scheme in my database:
{
"users" : {
"3pwcQ0VuzogVmmQ97yDExkMac1m1" : {
"Email" : "greg@gmail.com",
"code" : "bob",
"image" : "",
"location" : "fl"
}
}
}
React JS:
state = {
email: '',
password: '',
code: 'bob',
location: 'fl',
image: null,
url: '',
error: null,
};
handleInputChange = e => {
this.setState({ [e.target.name]: e.target.value });
};
handleChange = e => {
if (e.target.files[0]) {
const image = e.target.files[0];
this.setState(() => ({image}));
}
}
handleSubmit = (event) => {
event.preventDefault();
const { email, password, image } = this.state;
const uploadTask = storage.ref(`images/${image.name}`).put(image);
uploadTask.on('state_changed', () => {
// complete function ....
storage.ref('images').child(image.name).getDownloadURL().then(url => {
console.log(url);
this.setState({url});
})
});
firebase
.auth()
.createUserWithEmailAndPassword(email, password)
.then((user) => {
firebase
.database()
.ref('users/' + user.user.uid)
.set({
Email: user.user.email,
code: this.state.code,
location: this.state.location,
image: this.state.url
})
this.props.history.push('/');
})
.catch((error) => {
this.setState({ error: error });
});
};