【问题标题】:Status 500 "Error: Could not handle the request" for every other request其他所有请求的状态 500“错误:无法处理请求”
【发布时间】:2021-05-09 20:30:46
【问题描述】:

我已经制作了一个 Flutter 应用程序,并且刚刚开始使用 Cloud Functions 向其他手机发送推送通知。我通过 API 调用调用 Cloud Function 来工作,也就是说,消息被发送,我得到响应代码 200。但有趣的是,大约每隔一段时间(有时更多,有时更少)我得到回复:

状态码:500,正文:“错误:无法处理请求”

这特别奇怪,因为目前,我每次都发送完全相同的消息!...就像是完全相同的 API 调用,没有标头和完全相同的正文。然而,我得到不同的结果。可能是什么问题?

这是我的云函数:

const functions = require("firebase-functions");
const admin = require("firebase-admin");

exports.sendMsg = functions.https.onRequest((request, response) => {

  var decodedBody = JSON.parse(request.body);
  const message = decodedBody;

  // Send a message to the device corresponding to the provided
  // registration token:
  admin.initializeApp();
  admin.messaging().send(message)
      .then((res) => {
        // Response is a message ID string.
        console.log("Successfully sent message:", res);
        response.send("Message sent!\n\n" + res);
      })
      .catch((error) => {
        console.log("Error sending message:", error);
        response.send("Error sending message:\n\n" + error);
      });
});

这是我的 API 调用:

import 'dart:convert';
import 'my_firebase.dart';
import 'dart:io';
import 'package:firebase_messaging/firebase_messaging.dart';

//...//
void msgFunction(){
  final FirebaseMessaging _fcm = FirebaseMessaging();
         await MyFirebase.myFutureFirebaseApp;
          String fcmToken = await _fcm.getToken();
          http.Response res;

          try {
            res = await http.post(
              'https://us-central1-<my_project>.cloudfunctions.net/sendMsg',
              body: jsonEncode({
                "notification": {
                  "title": "This is my title",
                  "body": "Accept Ride Request",
                },
                "data": {
                  "score": "850",
                  "time": "245",
                  "click_action": "FLUTTER_NOTIFICATION_CLICK",
                  "id": "1",
                  "status": "done",
                },
                "token": "$fcmToken",
              }
              ),
            );
          } catch (e) {
            print('Caught an error in API call!');
            print('e is: ${e.toString()}');
            if (res != null) print('Status code in apiCall() catch is ${res.statusCode}');
          }
}

执行失败的在线函数日志:

Function execution started
Function execution took 173 ms, finished with status: 'crash'

【问题讨论】:

    标签: javascript flutter google-cloud-functions firebase-cloud-messaging http-status-code-500


    【解决方案1】:

    自己解决了! ?

    问题显然是admin.initializeApp(); 命令,我有点怀疑...因为在某些时候,日志告诉我这个函数被调用了两次。

    解决方案是把它放在云函数之外,而不是里面!像这样:

    const functions = require("firebase-functions");
    const admin = require("firebase-admin");
    admin.initializeApp();
    
    exports.sendMsg = functions.https.onRequest((request, response) => {
    
      var decodedBody = JSON.parse(request.body);
      const message = decodedBody;
    
      // Send a message to the device corresponding to the provided
      // registration token:
      admin.messaging().send(message)
          .then((res) => {
            // Response is a message ID string.
            console.log("Successfully sent message:", res);
            response.send("Message sent!\n\n" + res);
          })
          .catch((error) => {
            console.log("Error sending message:", error);
            response.send("Error sending message:\n\n" + error);
          });
    });
    

    这是视频号。本视频系列教程中有关 Firebase 的 Cloud Functions 的 2 篇文章让我明白了:

    https://firebase.google.com/docs/functions/video-series

    这是一个很好的系列!我可以推荐它。

    和平! ✌️

    【讨论】:

      猜你喜欢
      • 2015-08-28
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 2020-05-29
      • 1970-01-01
      相关资源
      最近更新 更多