Firebase未处理错误范围错误:超出最大调用堆栈大小

mqkwyuun  于 2023-02-25  发布在  其他
关注(0)|答案(3)|浏览(171)

我正在调用一个Firebase可调用函数,并返回Firebase指定的promise,但我得到了这个错误:
未处理的错误范围错误:超出最大调用堆栈大小
下面是代码:

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 ":{"描述":"此为基本款产品","价格":40}}
node:{"描述":"此为基础产品","价格":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)

救命!

2w2cym1i

2w2cym1i1#

这个问题类似于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和callback,只使用promise会更容易。

fzwojiic

fzwojiic2#

我的解决方案是用承诺来 Package 退货:

exports.getIssue = functions.https.onCall(data => {
const issueKey = data.issueKey

return new Promise((resolve, reject) => {
    admin
        .database()
        .ref('issue')
        .child(issueKey)
        .once('value', snapIssue => {
            let issue = snapIssue.val()
            if (issue) {
                return resolve(issue)
            } else {
                reject('Issue not found')
            }
        })
        .catch(reject)
})
u91tlkcl

u91tlkcl3#

值得一提的是,这一点在Firebase文档中并没有明确写出来;在HTTP请求之前,你应该总是字符串化对象。另一方面,你只需要解析它。删除JSON.stringify()和JSON.parse()将触发完全相同的错误。
客户端:

auth.signInWithEmailAndPassword(username, password).then(async (snapShot) => {
    const getToken = await functions.httpsCallable('getToken')(JSON.stringify(snapShot));
    ....
});

服务器端:

exports.getToken = functions.https.onCall(async (data, context) => {
    console.log(JSON.parse(data));
    ...
});

相关问题