【问题标题】:Flutter and Firebase Functions: TypeError: Cannot read property 'token' of undefinedFlutter 和 Firebase 函数:TypeError:无法读取未定义的属性“令牌”
【发布时间】:2021-07-14 01:20:50
【问题描述】:

这是我在调试时遇到的错误

[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: [firebase_functions/3840] The data couldn’t be read because it isn’t in the correct format.
#0      catchPlatformException
package:cloud_functions_platform_interface/…/utils/exception.dart:21
#1      _rootRunBinary (dart:async/zone.dart:1378:47)
#2      _CustomZone.runBinary (dart:async/zone.dart:1272:19)
#3      _FutureListener.handleError (dart:async/future_impl.dart:166:20)
#4      Future._propagateToListeners.handleError (dart:async/future_impl.dart:716:47)
#5      Future._propagateToListeners (dart:async/future_impl.dart:737:24)
#6      Future._completeError (dart:async/future_impl.dart:547:5)
#7      _completeOnAsyncError (dart:async-patch/async_patch.dart:264:13)

而且函数日志有这个错误:

Unhandled error TypeError: Cannot read property 'token' of undefined
    at /workspace/index.js:19:33
    at func (/workspace/node_modules/firebase-functions/lib/providers/https.js:273:32)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) 

实际功能:

const functions = require("firebase-functions");
const firebaseTools = require("firebase-tools");

exports.delMessages = functions.runWith({
  timeoutSeconds: 250,
  memory: "512MB",
}).https.onCall(async (data, context) => {
  if (!(context.auth && context.auth.token && context.auth.token.admin)) {
    throw new functions.https.HttpsError(
        "permission-denied", "user must be logged in"
    );
  }

  const path = data.path;
  await firebaseTools.firestore.delete(path, {
    project: process.env.GCLOUD_PROJECT,
    recursive: true,
    yes: true,
    token: functions.config().fb.token,
  });

  return {
    path: path,
  };
});

以及颤振调用:

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cloud_functions/cloud_functions.dart';

import './message.dart';

class ChatList extends StatefulWidget {
  ChatList({
    Key key,
  }) : super(key: key);

  @override
  _ChatListState createState() => _ChatListState();
}

class _ChatListState extends State<ChatList> {
  Future<void> runDelMessage(String msgId) async {
    final functions = FirebaseFunctions.instance;
    HttpsCallable callable = functions.httpsCallable('delMessage');
    await callable({'path': 'chat/$msgId'});
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: FirebaseFirestore.instance
          .collection('chat')
          .orderBy(
            'createdAt',
            descending: true,
          )
          .snapshots(),
      builder: (ctx, AsyncSnapshot<QuerySnapshot> chatSnapshot) {
        if (chatSnapshot.connectionState == ConnectionState.waiting) {
          return Expanded(
            child: Center(
              child: CircularProgressIndicator(),
            ),
          );
        }

        if (chatSnapshot.hasError) {
          print('there was an error');
        }
        final chatDocs = chatSnapshot.data.docs;
        final int chatLength = chatDocs.length;
        //final displayDocs = chatDocs.sublist(chatLength - 30);
        if (chatLength > 50) {
          runDelMessage(chatDocs[chatLength - 1].id);
        }
        return Expanded(
          child: ListView.builder(
            //controller: _scrollController,
            reverse: true,
            itemCount: chatLength,
            itemBuilder: (context, index) {
              return Message(
                chatDocs[index].data(),
                key: ValueKey(chatDocs[index].id),
              );
            },
          ),
        );
      },
    );
  }
}

我查过官方FlutterFire docs,代码大多是Firebase Functions example的副本。该代码表明用户令牌将由 SDK 处理。我该怎么办?

谢谢

【问题讨论】:

    标签: firebase flutter


    【解决方案1】:

    问题是如果 Map 有一个键,则请求应该是一个列表。

    令牌也需要作为数据传递。

    【讨论】:

      猜你喜欢
      • 2018-03-07
      • 2021-09-06
      • 1970-01-01
      • 2018-01-22
      • 2018-09-19
      • 2021-01-25
      • 2021-11-16
      • 2021-12-15
      • 2020-08-01
      相关资源
      最近更新 更多