带有参数的Flutter Cloud函数调用

时间:2019-09-01 10:16:41

标签: flutter google-cloud-functions

我有一个Flutter应用,我的代码如下:

  dynamic result = await CloudFunctions.instance.getHttpsCallable(functionName: 'getAllItemsAfterDatetime').call(
    <String, dynamic> {"datetime": "1563109861142"}
  );

和一个JS文件:

exports.getAllItemsAfterDatetime= functions.https.onRequest(async (req, res) => {
  if (!mysqlPool) {
    mysqlPool = mysql.createPool(mysqlConfig);
  }

  let datetime = req.body.datetime;
  if (datetime == null) datetime = req.query.datetime;
  if (datetime == null) datetime = req.params.datetime;
  if (datetime == null) datetime = "3100000012120000";

  mysqlPool.query('SELECT * FROM items as res WHERE datetime > ' + datetime, (err, results) => {
    if (err) {
      console.error(err);
      res.status(500).send(err);
    } else {
      res.status(200).send({cnt: res.length, data: JSON.stringify(results)});
    }
  });

我总是得到“ 3100000012120000”,所以基本上我无法从mobil获得日期时间参数。它正在浏览器中工作。如何在mobil应用程序中执行此操作?

req.parameters是正确的单词或是什么?

谢谢

更新:解决方案

  //from browser
  let datetime = req.body.datetime; 

  //from mobile
  if (datetime == null) datetime = req.body.data.datetime; 

  //if not set
  if (datetime == null) datetime = "3100000012120000";

1 个答案:

答案 0 :(得分:1)

解决方案是

 //from browser
  let datetime = req.body.datetime; 

  //from mobile
  if (datetime == null) datetime = req.body.data.datetime; 

  //if not set
  if (datetime == null) datetime = "3100000012120000";