我正在使用 Expo、Firebase Cloud Function 和 Stripe 開發應用程式,由于某種原因,我第一次導航到螢屏CardPayment時代碼回傳錯誤:[Unhandled promise rejection: SyntaxError: JSON Parse error: Unexpected identifier "Error"],但如果我重繪 螢屏,代碼運行完美。任何人都知道為什么以及如何解決它?我已經嘗試了很多東西,甚至使用 axios,但似乎沒有任何效果。
后端
exports.addCardForExistingCustomer = functions.https.onRequest(async (req, res) => {
const ephemeralKey = await stripe.ephemeralKeys.create(
{customer: `${req.query.customer}`},
{apiVersion: '2020-08-27'}
);
const setupIntent = await stripe.setupIntents.create({
customer: `${req.query.customer}`
});
const paymentMethods = await stripe.paymentMethods.list({
customer: `${req.query.customer}`,
type: 'card',
});
res.json({
setupIntent: setupIntent.client_secret,
ephemeralKey: ephemeralKey.secret,
customer: `${req.query.customer}`,
paymentMethods: paymentMethods,
})
});
前端
export default function CardPayment() {
const { initPaymentSheet, presentPaymentSheet } = useStripe();
const [loading, setLoading] = useState(false);
const [customerId, setCustomerId] = useState('');
const fetchPaymentSheetParams = async () => {
const response = await fetch(`https://<<path>>.cloudfunctions.net/addCardForExistingCustomer?customer=${customerId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
});
const { setupIntent, ephemeralKey, customer } = await response.json();
return {
setupIntent,
ephemeralKey,
customer,
};
};
const initializePaymentSheet = async () => {
const {
setupIntent,
ephemeralKey,
customer,
} = await fetchPaymentSheetParams();
console.log(setupIntent)
const { error } = await initPaymentSheet({
customerId: customer,
customerEphemeralKeySecret: ephemeralKey,
setupIntentClientSecret: setupIntent,
});
if (!error) {
setLoading(true);
} else {
console.log(error)
}
};
const openPaymentSheet = async () => {
const { error } = await presentPaymentSheet();
if (!error) {
Alert.alert('Success', 'Your payment method is successfully set up for future payments!');
}
};
const fetchCustomer = async () => {
const fetchInformation = doc(db, 'clients', 'user', 'Information', auth.currentUser.uid)
await getDoc(fetchInformation)
.then((snapshot) => {
const stripeId = snapshot.data().stripeCustomerId
setCustomerId(stripeId)
initializePaymentSheet();
})
}
useEffect(() => {
fetchCustomer()
}, []);
if (loading == false) {
return (
<View>
<ActivityIndicator/>
</View>
)
} else {
return (
<View>
<CustomButton
buttonTxt={'Add Credit/Debit Card'}
txtColor={'white'}
backgroundColor={'black'}
onPress={openPaymentSheet}
/>
</View>
)
}
}
uj5u.com熱心網友回復:
fetchCustomer在前端不等待getDoc承諾鏈完成。嘗試將returnor放在await呼叫到 之前getDoc。如果return使用,fetchCustomer則發布的內容不需要是async函式。
如果這解決了錯誤,我的猜測是某種回應快取允許then處理程式getDoc(fetchInformation)在重新加載頁面時贏得承諾作業處理(在微任務佇列中)中設定的競爭條件。
getDoc呼叫處理程式 ( )在呼叫之前async (snapshot) => {...}缺少一個await運算子initializePaymentSheet();。initializePaymentSheet同樣,如果延遲,這可能會在微任務佇列中設定不可預測的競爭條件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/445833.html
標籤:javascript 节点.js json 反应式 谷歌云功能
上一篇:_InternalLinkedHashMap<String,dynamic>'不是'FutureOr<List<dynamic>>型別的子型別
