【问题标题】:Why my Firestore transaction called via Firebase Functions is timing out?为什么我通过 Firebase 函数调用的 Firestore 事务超时?
【发布时间】:2018-09-02 23:11:05
【问题描述】:

在函数日志中,我可以看到它正在运行并且更新已应用于文档,但是它正在超时。

日志显示:

11:41:47.474 PM addContact 函数执行耗时 60003 毫秒,完成 状态:“超时”

11:40:47.743 PM addContact 交易成功!

11:40:47.659 PM addContact 9 9

11:40:47.659 PM 添加联系方式

11:40:47.659 PM addContact 8 8

11:40:47.656 PM 添加联系方式

11:40:47.472 PM addContact 函数执行开始

exports.addContact = functions.https.onRequest((req, res) => {

let user_id="fhr..."
let asker_id="wnz...";
let giver_id="Sp7...";

//const requestRef = admin.firestore().collection('contactRequests').doc(user_id)
const askerRef = admin.firestore().collection('users').doc(asker_id)
const giverRef = admin.firestore().collection('users').doc(giver_id)


var transaction = admin.firestore().runTransaction(t => {
  return t.getAll(giverRef, askerRef)
    .then(docs => {
      const id1 = docs[0];
      const id2 = docs[1];

      if ((id1.exists && id2.exists)) {
        // do stuff

        let giverContacts = docs[0].data().foo
        let askerContacts = docs[1].data().foo

        console.log("****")
        console.log(giverContacts, askerContacts)

        giverContacts=giverContacts+1
        askerContacts=askerContacts+1

        console.log("****")
        console.log(giverContacts, askerContacts)

        t.update(giverRef, {foo: giverContacts})
        t.update(askerRef, {foo: askerContacts})

        return true
      } else {
        throw new Error();
      }

    })
})
.then(result => console.log('Transaction success!') )
.catch(err => console.log('Transaction failure:', err) );

return transaction;

});

【问题讨论】:

    标签: firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    超时是因为您从未向客户端发送响应。 HTTP 类型函数仅在发送响应后终止。你可以发送任何东西:

    .then(result => {
        console.log('Transaction success!')
        res.send("OK")  // send anything at all to terminate the function
    })
    

    对于所有其他类型的函数,您必须在所有工作完成后返回一个 promise the resolves。

    read the documentation了解更多信息。

    【讨论】:

    • 感谢您的回答。我忽略了关于 http 类型函数的部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-18
    相关资源
    最近更新 更多