node.js - Firebase功能错误; "Maximum call stack size exceeded"

标签 node.js typescript firebase lodash google-cloud-firestore

我创建了一个函数,可以通过事务切换数据库中的 bool 值。看看:

export const toggleDoneState = functions.https.onCall((data, context) => {
    const userId = getUserIdFromCallableContext(context)
    let togglePath: admin.database.Reference

    if (!isUndefined(data.subtaskId)) {
        const subtaskId = data.subtaskId
        if (isValidString(subtaskId)) {
            togglePath = admin.database().ref("userSubtasks").child(userId).child(subtaskId).child("done")
        } else {
            throw new functions.https.HttpsError('invalid-argument', 'Expected valid Subtask Id')
        }
    } else {
        throw new functions.https.HttpsError('invalid-argument', 'Expected valid toggle type')
    }

    return togglePath.transaction(currentValue => {
        return !(currentValue || false)
    }).then(value => { 
        return { done: value }
    })
})

这是我部署的唯一函数!我曾经运行过大约 15 个函数,但我已删除它们以使此测试更清晰。

当我从 iOS 应用程序调用该函数时,我看到数据库中的值按预期切换,但我收到来自 iOS 中的 Functions SDK 的错误:

Domain=com.firebase.functions Code=13“INTERNAL”UserInfo={NSLocalizedDescription=INTERNAL}

当我查看控制台中的函数日志时,我看到以下错误:

Unhandled error RangeError: Maximum call stack size exceeded

at Function.mapValues (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:13395:23)

at encode (/user_code/node_modules/firebase-functions/lib/providers/https.js:204:18)

at /user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:13400:38

at /user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:4925:15

at baseForOwn (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:3010:24)

at Function.mapValues (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:13399:7)

at encode (/user_code/node_modules/firebase-functions/lib/providers/https.js:204:18)

at /user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:13400:38

at /user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:4925:15

at baseForOwn (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:3010:24)

at Function.mapValues (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:13399:7)

数据库中的值按预期切换,但我不想出现错误并理解原因。有什么线索吗?

最佳答案

我怀疑您对交易后 then 回调中的 value 存在误解。如果您查看 transaction() 的 API 文档,您将看到它返回一个包含几个属性的 Promise,其中之一是 DataSnapshot。您正在有效地尝试序列化该 DataSnapshot 对象,并且我认为 lodash 遇到了问题,可能是循环引用。

首先,尝试修复 then 回调的返回值,看看是否可以解决问题。然后,弄清楚如何使用事务生成的 DataSnapshot 将您想要的值返回给客户端。

关于node.js - Firebase功能错误; "Maximum call stack size exceeded",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50665804/

相关文章:

javascript - TypeScript 在静态上下文中从类创建新实例

javascript - 将数组列表转换为对象数组

ios - firebaseobserveSingleEvent 在 swift 中已弃用

node.js - 如何在nodejs客户端设置token?

node.js - 从对象中创建列表项

angular - 如何在 Spartacus 2.1 中使用类型增强?

java - 如果用户不存在如何创建?

firebase - 如何验证用户当前密码?

javascript - 如何在Loopback中获取Model内的请求参数

javascript - 仅当数组长度达到 X 个元素时执行