【问题标题】:Firebase cloud function not returning a value, return data is always nullFirebase云函数不返回值,返回数据始终为空
【发布时间】: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


【解决方案1】:

感谢 Frank 和 Doug 的建议。 我通过返回一个承诺解决了这个问题。另外,我意识到我必须在关闭 http 可调用云函数之前返回承诺,而不是在任何内部异步方法中......

我正在为可能面临同样问题的任何人发布我更正的代码

exports.checkDewBubble = functions.https.onCall( (data) => {

//some code including async functions

const ref = db.collection("users").doc(userID).collection("results").doc("dpbp")
        return ref.update({
                bubblePoint: parseFloat(bubblePoint),
                dewPoint: parseFloat(dewPoint)
            })
            .then(() => {   

                return {
                    dew: (dewPoint - 273.15).toFixed(2),
                    bubble: (bubblePoint - 273.15).toFixed(2)
                }
            })
            .catch((err) => {
                throw new functions.https.HttpsError('failed to connect' + err.message)

            })
          
})

这会将对象返回到客户端并因此解析。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-27
    • 2019-09-29
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2020-05-16
    相关资源
    最近更新 更多