【发布时间】:2021-11-24 19:10:31
【问题描述】:
我正在使用下面的代码从 Firebase 返回一个对象,但该值始终为 null。
这是我的 Firebase 云功能:
exports.useVoucher = functions.https.onCall((data, context) => {
const type = data.type
const voucher_code = data.voucher_code
const current_time = data.current_time
console.log("useVoucher type is ", type + " and voucher_code is ", voucher_code)
return admin.database().ref().child("vouchers").child(voucher_code).once("value").then((snapshot) => {
if (snapshot.exists()) {
console.log("useVoucher snapshot is ", snapshot.val());
if (snapshot.val()[type] === true) {
let path = "vouchers/" + voucher_code + "/" + type
admin.database().ref().update({
[[path]]: current_time
}, error => {
if (error) {
console.log("useVoucher failed")
return {is_valid: false}
} else {
console.log("useVoucher succeeded")
return {is_valid: true}
}
})
} else {
console.log("useVoucher voucher found but type not found or not true")
return {is_valid: false}
}
} else {
console.log("useVoucher voucher_code is not valid");
return {is_valid: false}
}
})
})
这就是我所说的客户端(我删除了一些不相关的代码):
async function testVoucher(type) {
const voucher_code = input_text.value.trim()
console.log("submitVoucher voucher_code is ", voucher_code + " type is ", type)
let current_time = Number(new Date())
console.log("submitVoucher current_time is ", current_time)
await useVoucher({ type: type, voucher_code: voucher_code, current_time: current_time })
.then((res) => {
console.log("submitVoucher res is ", res)
let data = res.data
console.log("submitVoucher data is ", data)
})
.catch((error) => {
console.log("submitVoucher error is ", error)
})
}
无论云函数采用哪条路径,它总是返回“res is null”。这是为什么呢?
【问题讨论】:
-
请在最后一个 sn-p(和一般情况下)中不要组合
await和then。使用其中一种。 -
您从 Cloud Function 和客户端获得什么日志输出?您可以将这些添加到您的问题中吗?
-
@FrankvanPuffelen 为什么不能合并它们?
标签: javascript firebase google-cloud-functions