简体   繁体   English

Firebase 云函数返回内部错误

[英]Firebase Cloud Function returns INTERNAL error

I am getting the error as INTERNAL when calling a Firebase Cloud Function from my app.从我的应用程序调用 Firebase Cloud Function 时,我收到INTERNAL错误。

Server code:服务器代码:

exports.addToCart = functions.https.onCall((data, context) => { 
  // Checking that the user is authenticated.
  if (!context.auth) {
    // Throwing an HttpsError so that the client gets the error details.
    throw new functions.https.HttpsError(
      "failed-precondition",
      "The function must be called " + "while authenticated."
    );
  }

  // Get data
  const food = data.food;

  // Get user details
  const uid = context.auth.uid;

  console.log("User id " + uid);
  console.log("Food name " + food.name);

  return "Added to cart successfully."
});

Java Code: Java代码:

addToCartTask(foodItem)
  .addOnSuccessListener(s -> {
    Log.e(TAG, "Success : " + s);
  })
  .addOnFailureListener(e -> {
    Log.e(TAG, "Error : " + e.getLocalizedMessage());
  });

private Task<String> addToCartTask(Food foodItem) {
  // Create the arguments to the callable function.
  Map<String, Object> objectHashMap = new HashMap<>();
  objectHashMap.put("foodItem", foodItem);

  Gson gson = new Gson();
  String data = gson.toJson(objectHashMap);

  return firebaseFunctions
    .getHttpsCallable("addToCart")
    .call(data);
  }

The error respose is due to accessing the properties of the custom java object passed in the function.错误响应是由于访问函数中传递的自定义 java 对象的属性。

How to access the properties of the passed object properties?如何访问传递的对象属性的属性?

You are calling your function with a String , but accessing it as a JSONObject .您正在使用String调用您的函数,但将其作为JSONObject访问。

The documentation for call() shows that it accepts a range of types including String , Map<String,?> , and JSONObject . call()文档显示它接受一系列类型,包括StringMap<String,?>JSONObject That gives you some options for passing the food item and accessing it in your function.这为您提供了一些用于传递食品并在您的函数中访问它的选项。

  1. As you do now, convert the food item to a JSON string and pass the string.正如您现在所做的那样,将食品项目转换为 JSON 字符串并传递该字符串。 In the function code, you need to convert the string to an object with const food = JSON.parse(data) before accessing the fields, eg food.foodItem.name .在函数代码中,您需要在访问字段之前将字符串转换为具有const food = JSON.parse(data)的对象,例如food.foodItem.name

  2. If Food doesn't have many fields you could put each field in your objectHashMap and pass the map, without the conversion to JSON.如果Food没有很多字段,您可以将每个字段放入objectHashMap并传递地图,而无需转换为 JSON。 Then in your function code, you could access each field without the need to use JSON.parse() .然后在您的函数代码中,您可以访问每个字段而无需使用JSON.parse()

You could get an internal error in a case when the Function instance is turned off by Google Cloud.如果 Function 实例被 Google Cloud 关闭,您可能会遇到内部错误。 It could happen if you have billing issues, like not enabling billing after your free trial is over.如果您有计费问题,例如在免费试用结束后未启用计费,则可能会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM