我创建了一个函数,可以通过事务切换数据库中的 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/