【发布时间】:2020-01-02 19:36:37
【问题描述】:
我对 firebase 比较陌生,并且一直在尝试在执行云函数后返回一个值。我可以看到该功能运行顺利。它只是返回一个空数据。如何
这是从 firebase 云函数中获取结果。 httpCallable
无法从 Firebase 云函数接收值。这是我的功能代码。该功能运行顺利。只是它返回一个空数据。
exports.checkDewBubble = functions.https.onCall( (data) => {
const ref = db.collection("users").doc(userID).collection("results").doc("dpbp")
...
some calculation
...
ref.update({
enter code here
bubblePoint: bubblePoint - 273.15,
dewPoint: dewPoint - 273.15
})
.then(() => {
return {
dew: (bubblePoint - 273.15).toFixed(2),
bubble: (bubblePoint - 273.15).toFixed(2)
}
})
.catch((err) => {
throw new functions.https.HttpsError('failed to connect' + err.message)
})
这里是应用调用函数的代码......
let checkDewBubble = firebase.functions().httpsCallable('checkDewBubble')
checkDewBubble(data)
.then((result) => {
console.log(result)
this.dewPoint = result.data.dew
this.bubblePoint = result.data.bubble
const ref = db.collection("users").doc(userID).collection("results").doc("dpbp")
ref.get().then((doc)=>{
console.log(doc.data().dewPoint, doc.data().bubblePoint)
this.dewPoint = doc.data().dewPoint
this.bubblePoint = doc.data().bubblePoint
})
.then(()=>{
some code
})
.catch((error)=> {
console.log("Error getting document:", error)
this.$store.dispatch('setLoading',false)
// this.status = false
})`
我收到以下错误
Uncaught (in promise) TypeError: Cannot read property 'dew' of null"
我期待一个露点值。
【问题讨论】:
-
可调用函数需要返回一个在所有异步工作完成时解析的promise。您的函数实际上并没有向客户端返回值(没有顶级 return 语句)。从
then回调中返回值与在顶层返回 Promise 不同。 -
您好弗兰克,感谢您的回复。您是否建议如果我在更新数据库之前保留我的 return 语句,即在我的代码中的“ref.update”之前,它会起作用吗?
-
我会试试你的建议,很快就会告诉你进展如何。再次感谢
-
成功了!谢谢大家
标签: javascript firebase vue.js google-cloud-functions