【问题标题】:Firebase Cloud Function Triggered TwiceFirebase 云函数触发两次
【发布时间】:2018-01-05 07:56:34
【问题描述】:

我正在尝试将 Firebase Cloud Functions 集成到 Android 应用中。我是 Cloud Functions 和 Node.js 的新手。

所以,我有以下简单的 node.js 代码来将数据保存在 Firestore 数据库中:

'use strict';

const admin = require('firebase-admin');
const functions = require('firebase-functions');
admin.initializeApp(functions.config().firebase);
const Firestore = require('@google-cloud/firestore');


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

    var firestoreDB = new Firestore({
        projectId: 'my_project_id',
        keyFilename: 'my_key_file_name'
    });

    const line1 = request.body.line1;
    const line2 = request.body.line2;

    var data = {
        name: line1,
        number: line2
    };

    return firestoreDB.collection('myData').doc('firstDoc')
    .add( data )
    .then(ref => {
        console.log('Data saved.');
        response.end();
    });
});

这段代码运行良好,但被执行了两次。它将数据写入数据库两次,在 Firebase 控制台日志屏幕中记录每个日志两次。

我尝试了另一个使用 FCM 从 Android 设备向 Android 设备发送消息的功能,它也发送了两次消息。

我检查了 Android 端以确保它被触发一次。我想知道是否有什么我错过了让它工作。任何帮助或建议表示赞赏。

【问题讨论】:

  • 由于我在您的代码中没有看到任何响应并且它以超时结束,因此您的设备可能会在超时后发送另一个请求。我的浏览器有这个问题,在超时后发送了另一个请求。在console.log('Data saved.'); 之后,您不是缺少response.status(200).send('OK') 之类的东西吗?不过,我不熟悉 firebase 功能。 更新:刚刚找到一个例子here
  • 我不确定这是否是根本原因,但您应该使用 res.redirect()、res.send() 或 res.end() 解决前提。
  • @Molda,我认为我的设备在超时后不会发送另一个请求。上述超时消息在一定时间后同时出现。我在一些教程中看到了'response.status(200).send('OK')',它有什么作用以及它应该在代码中的什么位置?谢谢。
  • @Dan,您能否通过代理(如 Fiddler)引导来自您设备的流量,以验证该应用仅发出一个请求。

标签: android node.js google-cloud-functions


【解决方案1】:

您没有从 API 发送响应,因此浏览器没有收到任何响应,它会重试 API,直到超时 60 秒。

response.status(200).send('OK');

输入上面的这一行,如下所示 -

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

    var firestoreDB = new Firestore({
        projectId: 'my_project_id',
        keyFilename: 'my_key_file_name'
    });

    const line1 = request.body.line1;
    const line2 = request.body.line2;

    var data = {
        name: line1,
        number: line2
    };

    return firestoreDB.collection('myData').doc('firstDoc')
    .add( data )
    .then(ref => {
        console.log('Data saved.');
        response.status(200).send('OK');
    });
});

【讨论】:

  • 感谢您的代码。真正的问题在于 Android (Java) 端的Volley。默认情况下,Volley 如果在 2.5 秒内没有收到回复,则第二次尝试访问服务器。因为我操作繁重,有时网速下降,所以总是需要超过 2.5 秒才能回复。
【解决方案2】:

所以,真正的问题在于 Android (Java) 端的 Volley。默认情况下,如果 Volley 在 2.5 秒内没有收到回复,它会第二次尝试访问服务器。因为我操作繁重,有时网速下降,所以响应时间总是超过 2.5 秒。

如果有人遇到类似问题,请仔细检查 Volley 并确保在 Volley 请求的末尾添加以下内容

      stringRequest.setRetryPolicy(
            new DefaultRetryPolicy(
                    15000,   // timeout timimg of 15 sec
                    0,       // number of retries. By default the number is 1
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    // Add the request to the RequestQueue.
    MySingleton.getInstance().addToRequestQueue(stringRequest);

【讨论】:

  • 你把这个放在哪里?我的执行时间不到 1 秒
猜你喜欢
  • 1970-01-01
  • 2018-01-08
  • 2018-09-14
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
  • 2021-02-13
  • 2021-10-27
相关资源
最近更新 更多