My problem
I have read several SO Q&A on this (like In Firebase when using push() How do I get the unique ID and store in my database) but cannot make my code to work.
What I am trying to do
After firebase.push
, to get the created key and read the data written in the push
call.
Here is the code:
addCard = async ({
number, expMonth, expYear, cvc,
}) => {
this.setState({ addingCardInProcess: true });
try {
const tokenObject = await stripe.createTokenWithCard({
number, expMonth, expYear, cvc
});
firebase
.database()
.ref(`/stripe_customers/${uid()}/sources`)
.push({ token: tokenObject.tokenId })
.then(() => {
// I want to get the key and read the pushed data here.
// How do I do it?
this.setState({ addingCardInProcess: false });
this.cardAlert(true);
})
.catch((err) => {
this.setState({ addingCardInProcess: false });
this.cardAlert(false, err.message);
});
} catch(err) {
this.cardAlert(false, err.message);
this.setState({ addingCardInProcess: false })
}
};