【发布时间】:2021-11-27 20:22:33
【问题描述】:
我有几个遵循此语法的 Firebase 云函数:
exports.sendText = functions.https.onCall((data, context) => {...
这总是很好用,但我一直在为我最近部署的这个新的“发布”功能而苦苦挣扎。这就是它的定义...
payment_app.post("/create-payment-intent", async (req, res) => {
const { items } = req.body;
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(items),
currency: "usd",
automatic_payment_methods: {
enabled: true,
},
});
res.send({
clientSecret: paymentIntent.client_secret,
});
});
exports.payment = functions.https.onRequest(payment_app)
这就是我从客户端调用它的方式。
function buyProduct(id, quantity) {
fetch('payment_app/create-payment-intent', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
items: [
{ id: id, quantity: quantity }
]
})
}).then(res => {
if (res.ok) return res.json()
return res.json().then(json => Promise.reject(json))
}).then(({ url }) => {
console.log(url)
Object.assign(document.createElement('a'), { target: '_blank', href: url }).click();
var refreshVerify = setInterval(() => {
let payment_status = localStorage.getItem("payment_status")
if (payment_status) {
clearInterval(refreshVerify)
localStorage.removeItem("payment_status")
if (payment_status === "success") {
console.log("payment success")
} else if (payment_status === "cancel") {
console.log("payment cancel")
} else {
console.log("error local_payment_status variable not valid")
}
}
}, 1000)
}).catch(e => {
console.error(e.error)
})
}
这样做会返回 404 not found 错误:
script.js:3849 POST http://localhost:1234/payment_app/create-payment-intent 404 (Not Found)
buyProduct @ script.js:3849
testFunction @ script.js:3845
(anonymous) @ script.js:1257
setTimeout (async)
showApp @ script.js:1242
parcelRequire.script.js.firebase/compat/app @ script.js:1209
newRequire @ script.75da7f30.js:47
(anonymous) @ script.75da7f30.js:81
(anonymous) @ script.75da7f30.js:120
script.js:3880 undefined
谁能帮我弄清楚我做错了什么?如果重要的话,这是我的 firebase.json。我想我这里可能有问题?
{
"functions": {
"engines": {
"node": "14"
},
"source": "functions"
},
"database": {
"rules": "database.rules.json"
},
"hosting": {
"public": "public",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "payment_app/**",
"function": "create-payment-intent",
"destination": "/index.html"
}
]
}
}
【问题讨论】:
标签: javascript firebase google-cloud-functions