【问题标题】:Https request to update data in an array更新数组中数据的 Https 请求
【发布时间】:2019-04-10 06:35:29
【问题描述】:

我之前就这件事问过问题,但还是有点卡住。我要解决的问题如下: - 每当添加文档时,都需要在(2 小时)后的某个时间进行更新

我目前的解决方法是: - 我使用 onCreate 创建文档的时间戳 - 我将文档附加到数组 - 我有另一个 https 函数,它每 1 分钟触发一次(作为 cron 作业),它检查数组中的项目并将当前时间戳与文档的时间戳(这是数据库中的一个变量)进行比较

我的代码如下:

   const array = []

   export const onTimerCreate = functions.firestore
   .document(`Message/{UserId}/{ChatRoomId}/{UserId_1}`)
   .onCreate((snapshot, context) => {
     const timestamp = admin.firestore.Timestamp.now().seconds
     array.push(snapshot)
     return snapshot.ref.update({ creationTime: timestamp})

   })

   export const testDelay = functions.https.onRequest((request,               response) => {
     const timestamp = admin.firestore.Timestamp.now().seconds
     array.forEach( snapshot => {
       const data = snapshot.data()
       const time = data.creationTime
       const delay = (data.delay) / 1000
       const actualTime = time + delay
       console.log(array)
       if (actualTime > timestamp) {
         return snapshot.ref.update({ delay : 0})
       }
     })
   })

但是,现在的问题是请求在 60 秒后才超时。我认为正在发生的事情是我没有正确附加文档,因为我使用第一个函数向其中添加内容的数组没有保存数据。但是我不确定如何解决这个问题。我们将不胜感激。

【问题讨论】:

    标签: javascript typescript firebase google-cloud-firestore


    【解决方案1】:

    您的 testDelay 函数将始终超时,因为它从不向客户端发送响应。 According to the documentation:

    使用 res.redirect()、res.send() 或 res.end() 终止 HTTP 函数。

    如果您不对响应对象调用这些方法之一,Cloud Functions 将不会终止您的函数,并且会超时。

    请注意,如果您在 HTTP 类型函数中执行异步工作(例如文档更新),则必须仅在这些异步操作的所有承诺都得到解决后才发送响应。

    【讨论】:

    • 元素是否正确更新到数组中?即使它来自两个不同的功能?就像创建文档时,它会被上传到数组中,然后通过快照从 http 函数中读取数组?
    • 看文档就知道了,对吧?
    • 我在 if 语句中添加了这个:response.send(snapshot.ref.update({ delay : 0})),然后我在 else 语句中添加了 response.end()。虽然还是不行
    猜你喜欢
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 2019-09-30
    • 2019-02-10
    相关资源
    最近更新 更多