gpt4 book ai didi

Firebase 未处理的错误 RangeError : Maximum call stack size exceeded

转载 作者:行者123 更新时间:2023-12-02 04:45:10 29 4
gpt4 key购买 nike

我正在调用 Firebase 可调用函数,并返回 Firebase 指定的 promise ,但收到此错误:

未处理的错误 RangeError:超出最大调用堆栈大小

代码如下:

exports.getProductInfo = functions.https.onCall((data, context) => {
// Message text passed from the client.
const product = data.product;
// Authentication / user information is automatically added to the request.
// const uid = context.auth.uid;

// Get the current Server Timestamp
var ts = String(Date.now());
var key, snap, node;

// Get the price of the specified product
return database.ref('/products/' + product)
.orderByKey()
.endAt(ts)
.limitToLast(1)
.once('value', function (snapshot) {
snap = snapshot.val();
console.log('snap: ' + JSON.stringify(snap));

key = Object.keys(snap)[0];
node = snap[key];
console.log('node: ' + JSON.stringify(node));

return(node);
});
});

这是我在函数日志中看到的输出:

snap: {"1538004276":{"description":"这是基本产品","price":40}}

节点:{“description”:“这是基本产品”,“price”:40}

Unhandled error RangeError: Maximum call stack size exceeded
at Object (native)
at /user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:4905:24
at baseForOwn (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:2996:24)
at Function.mapValues (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:13401:7)
at encode (/user_code/node_modules/firebase-functions/lib/providers/https.js:242:18)
at /user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:13402:38
at /user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:4911:15
at baseForOwn (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:2996:24)
at Function.mapValues (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:13401:7)
at encode (/user_code/node_modules/firebase-functions/lib/providers/https.js:242:18)
at /user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:13402:38
at /user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:4911:15
at baseForOwn (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:2996:24)
at Function.mapValues (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:13401:7)
at encode (/user_code/node_modules/firebase-functions/lib/providers/https.js:242:18)
at /user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:13402:38
at /user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:4911:15
at baseForOwn (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:2996:24)
at Function.mapValues (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:13401:7)
at encode (/user_code/node_modules/firebase-functions/lib/providers/https.js:242:18)
at /user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:13402:38
at /user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:4911:15
at baseForOwn (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:2996:24)
at Function.mapValues (/user_code/node_modules/firebase-functions/node_modules/lodash/lodash.js:13401:7)

救命!

最佳答案

问题类似于the one described here 。您将返回一个由 Firebase API 生成的复杂对象,称为 DocumentSnapshot。快照本身并不是返回给客户端的原始 JSON 数据,它包含对其他对象的循环引用。 Cloud Functions 在尝试序列化所有这些对象时陷入困境。相反,只需通过在快照上调用 val() 返回感兴趣位置处的数据的原始 JavaScript 对象即可:

return database
.ref('/products/' + product)
.orderByKey()
.endAt(ts)
.limitToLast(1)
.once('value') // once() returns a promise containing a snapshost
.then(snapshot => {
return snapshot.val() // this is the raw JS object
})

您通常不会在同一调用中同时使用返回的 promise 回调。仅使用 Promise 会更容易。

关于Firebase 未处理的错误 RangeError : Maximum call stack size exceeded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52561349/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com