Javascript 在firebase.push()之后读取创建的密钥和数据 我的问题
我已经读了好几篇关于这方面的问答(比如),但无法使我的代码正常工作 我想做什么 在Javascript 在firebase.push()之后读取创建的密钥和数据 我的问题,javascript,firebase,react-native,firebase-realtime-database,Javascript,Firebase,React Native,Firebase Realtime Database,我已经读了好几篇关于这方面的问答(比如),但无法使我的代码正常工作 我想做什么 在firebase.push之后,获取创建的密钥并读取push调用中写入的数据 代码如下: 现在,在'id'变量中有了自动生成的键,在'body'变量中有了推送的数据 希望有帮助 addCard = async ({ number, expMonth, expYear, cvc, }) => { this.setState({ addingCardInProcess: true });
firebase.push
之后,获取创建的密钥并读取push
调用中写入的数据
代码如下:
现在,在'id'变量中有了自动生成的键,在'body'变量中有了推送的数据
希望有帮助
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 })
}
};
addCard = async ({
number, expMonth, expYear, cvc,
}) => {
this.setState({ addingCardInProcess: true });
try {
const tokenObject = await stripe.createTokenWithCard({
number, expMonth, expYear, cvc
});
let id = firebase.database().ref().push().key;
let body = { token: tokenObject.tokenId };
firebase
.database()
.ref(`/stripe_customers/${uid()}/sources`).child(id)
.set(body)
.then(() => {
// I want to get the key and read the pushed data here.
// Now you have your key here in "id" variable and data in "body" variable
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 })
}
};