首页 > 解决方案 > TypeError:在云函数中将循环结构转换为 JSON

问题描述

我正在尝试使用我的颤振应用程序中的 firebase 云功能,但是当我调用它时出现错误:

Unhandled error TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Socket'
    |     property 'parser' -> object with constructor 'HTTPParser'
    --- property 'socket' closes the circle
    at JSON.stringify (<anonymous>)
    at write (/workspace/node_modules/firebase-functions/lib/logger.js:12:84)
    at Object.log (/workspace/node_modules/firebase-functions/lib/logger.js:45:5)
    at /workspace/index.js:23:22
    at func (/workspace/node_modules/firebase-functions/lib/providers/https.js:273:32)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

我的功能如下所示:

exports.sendNotif = functions.https.onCall((listId, convName, message) => {
    const payload = {
        notification: {
            title: convName,
            body: message,
            badge: '1',
            sound: 'default'
        }
    }
    listId.forEach(id => {
        functions.logger.log(id);
        admin.firestore().collection('USERS').doc(listId).get().then(snapshot => {
            console.log(snapshot.data().FirebaseToken)
            admin.messaging().sendToDevice(snapshot.data().FirebaseToken, payload).then(response => {
                console.log('SUCCES')
            }).catch(error => { console.log('ERROR') })
        })
    });
})

我这样称呼它:

  sendNotifications() async {
    HttpsCallable callable =
        FirebaseFunctions.instance.httpsCallable('sendNotif');
    await callable.call(
      <String, dynamic>{
        'listId': ['10571112'],
        'convName': 'VYSgames',
        'message': 'Test'
      },
    );
  }

标签: node.jsfirebasefluttergoogle-cloud-functionsfirebase-cloud-messaging

解决方案


推荐阅读