处理超出期限的 Firebase Cloud Functions

Handle Deadline exceeded Firebase Cloud Functions

使用 firebase 函数在我的 firebase firestore 中插入数据。他们从不同的 API 获取数据。我编写并阅读了很多文档——这可能是我收到此错误的原因。 我也在 Github 上阅读了很多 Whosebug 帖子和问题,我发现这个错误可能有很多原因。我 99% 确定这与 Promises 无关。我正在使用 async / await

如何处理,有什么解决办法?

我捕捉到的确切错误: Error: 4 DEADLINE_EXCEEDED: Deadline exceeded Firebase Cloud Functions

这是 l1b3rty 询问的代码:


if (plans.results.length != 0) {
    const batch = db.batch()
    plans.results.forEach(async (plan) => {
        const jiraKey = plan.planItem.self.split('/').pop()
        let wyzioProjectId
        const projects = await db.collection('projects').where('jiraKey', '==', jiraKey).get()
        if (!projects.empty) {
            projects.forEach(async (project) => {
            wyzioProjectId = project.data().wyzioId
            const jiraUserId = plan.assignee.self.split('=').pop()
            const username = await user.findOne('user?accountId=' + jiraUserId)
            if (username != null) {
                let wyzioUserId
                const users = await db.collection('users').where('name', '==', username).get()
                if (!users.empty) {
                    users.forEach(user => {
                        wyzioUserId = user.data().id
                    })
                }
       batch.set(db.collection('plans').doc(JSON.stringify(plan.id)), {
                        'TempoId': plan.id,
                        'jiraKey': jiraKey,
                        'projectId': wyzioProjectId,
                        'userId': wyzioUserId,
                        'username': username,
                        'startDate': plan.startDate,
                        'values': plan.dates.values,
                        'status': 1,
                        'syncStatus': 1
                    }, { merge: true })
                }
            })
         } else {
            let key = "tempo_plan_" + plan.jiraWorkLogId + "_no_ref_project"
            let message = "Impossible d'ajouter le plan : " + plan.jiraWorkLogId + ", il n'appartient à aucun projet de la base de données"
            await notifications.addNotifications(db, key, message)
        }
    })
    try {
        await batch.commit()
        await incrementOffsetTempo(db, 'tempo_plans')
        console.log("--- tempo_plans_done ---")
    } catch {
        let key = "tempo_plan_" + plan.jiraWorkLogId + "_error_sync"
        let message = "Impossible d'ajouter le plan : " + plan.jiraWorkLogId + ", veuillez contrôler ses informations"
        await notifications.addNotifications(db, key, message)
    }
}

batch.set() 未正确对齐

我解决了用 for of 循环替换 foreach 循环的问题。这是一个例子:

if (plans.results.length != 0) {
    const batch = db.batch()
    for (const plan of plans.results) {
        const jiraKey = plan.planItem.self.split('/').pop()
        const projects = await db.collection('projects').where('jiraKey', '==', jiraKey).get()
        // ... 
        //set the batch with info
    }
    try {
        await batch.commit()
        await incrementOffsetTempo(db, 'tempo_plans')
        console.log("--- tempo_plans_done ---")
    } catch (error) {
        let key = "tempo_plans_error_sync"
        let message = "Impossible d'ajouter les plans. Erreur : " + error
        await notifications.addNotifications(db, key, message)
    }
}