简体   繁体   English

Firebase 云功能:等到request.body有效再更新

[英]Firebase Cloud Functions: Wait until request.body is valid before updating

I'm having some frustration with Cloud Functions accessed via my webGL Unity app.我对通过我的 webGL Unity 应用程序访问的 Cloud Functions 感到有些沮丧。 I am getting 2 requests instead of one, and the first one is always undefined.我收到 2 个请求而不是一个,第一个请求始终未定义。 Therefore my database is adding 2 elements to my array which each call, one completely empty, and one valid.因此,我的数据库向我的数组添加了 2 个元素,每个元素都调用,一个完全为空,一个有效。

Here is my cloud function, which works perfectly when tested from Unity, but not when run from my hosted site.这是我的云 function,从 Unity 测试时效果很好,但从我的托管站点运行时却不行。

export const addSavedMission = functions.https.onRequest(
    (request, response) => {
    response.set(
      "Access-Control-Allow-Origin",
      "https://myunity.web.app"
    );
    response.set("Access-Control-Allow-Headers", "*");
    response.set("Accept", "application/json");

    // update the array data
    admin
      .firestore()
      .doc("data/missionData")
      .update({
        missions: admin.firestore.FieldValue.arrayUnion(request.body),
      })
      .then(() => {
        response.status(200).json({ message: `Success` });
      })
      .catch((error) => {
        console.error(error);
        response.status(500).json({
          message: error,
        });
      });
});

And here is my Unity code which calls it这是我的 Unity 代码,它调用它

IEnumerator PostSavedData()
{
    string data = JsonUtility.ToJson(thisMission);

    string url = $"https://us-central1-app.cloudfunctions.net/addSavedMission";
    var request = new UnityWebRequest(url, "POST");
    byte[] bodyRaw = Encoding.UTF8.GetBytes(data);
    request.uploadHandler = new UploadHandlerRaw(bodyRaw);
    request.downloadHandler = new DownloadHandlerBuffer();

    request.SetRequestHeader("Access-Control-Allow-Methods", "POST");
    request.SetRequestHeader("Content-Type", "application/json");
    request.SetRequestHeader("Access-Control-Allow-Headers", "*");
    request.SetRequestHeader("Access-Control-Allow-Origin", "https://myunity.web.app");

    yield return request.SendWebRequest();
    if (request.isNetworkError)
    {
        Debug.Log("Error: " + request.error);
    }
    else
    {
        Debug.Log("Status Code: " + request.responseCode);           
    }

}

When I hit this POST request from my hosted app, I log out the incoming request.rawBody, and the first is always undefined, but the second one is valid.当我从我的托管应用程序中点击这个 POST 请求时,我注销了传入的 request.rawBody,第一个始终未定义,但第二个是有效的。

What am I missing here?我在这里想念什么? I'm sure there is 2 incoming requests from 1 POST.我确定有来自 1 个 POST 的 2 个传入请求。

I've tried checking to see if request.rawBody,== undefined, and only if it is not handling the firestore update but then it just never processes the 2nd valid request我试过检查request.rawBody,== undefined,并且只有当它没有处理firestore更新但它永远不会处理第二个有效请求

The first request is probably a cors pre-flight request .第一个请求可能是cors 飞行前请求 It will not contain the actual data of the request.它不会包含请求的实际数据。 It's just required by the cors protocol.它只是 cors 协议要求的。

Managing cors will be easier if you use the nodejs cors module in your Cloud Function so that it automatically handles the first request.如果您在 Cloud Function 中使用nodejs cors 模块,那么管理 cors 会更容易,以便它自动处理第一个请求。

const cors = require('cors')({origin: true});

export const addSavedMission = functions.https.onRequest(
    (request, response) => {
    cors(request, response, () => {
        // your function code here
    })
});

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

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