Firestore云功能:POST请求出现问题

时间:2019-03-01 09:33:17

标签: firebase firebase-realtime-database google-cloud-functions

我想使用Firestore云功能将数据发布到Firestore数据库。这是我的JavaScript代码:

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

admin.initializeApp();

exports.helloWorldJS = functions.https.onRequest((request, response) => {
    response.send("identifiant : " + request.body.identifiant);
});

exports.addMesure = functions.https.onRequest((req, res) => {
    const identifiant = req.body.identifiant;

    const mesure = {
        temperature: req.body.temperature,
        phSol: req.body.phSol
    };

    const db = admin.database().ref('/users/' + identifiant + '/mesures');
    db.push({mesure});
    res.send('mesure: ' + mesure);
});

当我使用Postman进行测试时,发送数据:

[{
"identifiant": "identifianttt",
"temperature": 35,
"phSol": 7
}]

helloWorldJS 函数返回: identifiant:未定义 并且 addMesure 函数返回:错误:无法处理请求

谢谢您的帮助。

2 个答案:

答案 0 :(得分:0)

对于第一个问题,您应仔细检查是否正确发送了尸体。

对于第二个Cloud Function,(在解决了主体的第一个问题之后),您应该调整代码以等待异步push()方法返回的promise解析后再发送回响应,如下所示:

exports.addMesure = functions.https.onRequest((req, res) => {
    const identifiant = req.body.identifiant;

    const mesure = {
        temperature: req.body.temperature,
        phSol: req.body.phSol
    };

    const dbRef = admin.database().ref('/users/' + identifiant + '/mesures');
    dbRef.push(mesure)  //No need to write {mesure}, since mesure is alreasdy a JS object
    .then(ref => {
         res.send('mesure: ' + mesure);
    })
    .catch(err => {....})

});

最后,我建议您观看Firebase视频系列中有关“ JavaScript Promises”的3个视频:https://firebase.google.com/docs/functions/video-series/。第一个完全满足您的情况(异步调用Firebase数据库,然后发送回响应)

答案 1 :(得分:0)

谢谢大家,一切正常,我将之前的代码更改为:

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

admin.initializeApp();

exports.helloWorld = functions.https.onRequest((req, res) => {
    res.send('identifiant: ' + req.body.identifiant);
});

exports.insertMesure = functions.https.onRequest((req, res) => {
    const identifiant = req.body.identifiant;
    const temperatureAir = req.body.temperatureAir;
    const humiditeSol = req.body.humiditeSol;
    const humiditeAir = req.body.humiditeAir;
    const niveauEau = req.body.niveauEau;
    const phSol = req.body.phSol;

    const mesure = {
        temperatureAir: temperatureAir,
        humiditeSol: humiditeSol,
        humiditeAir: humiditeAir,
        niveauEau: niveauEau,
        phSol: phSol
    };

    const dbRef = admin.database().ref('/users/' + identifiant + '/mesures');
    dbRef.push(mesure);
    // .then(snapchot => {
    //     const data = snapchot;
    //     return data;
    // })
    // .catch(err => {
    //     res.send('Error: ' + err);
    // });
});

我想使用以下代码通过 Arduino使用SIM800L模块发送数据:

#include <Sim800.h>
#include <Http.h>

#include <ArduinoJson.h>


#define BODY_FORMAT "{\"identifiant\":%s, \"temperatureAir\": %d, \"humiditeSol\": %d, \"humiditeAir\": %d, \"phSol\": %d, \"niveauEau\": %d}"

unsigned long lastRunTime = 0;
unsigned long waitForRunTime = 0;

unsigned int RX_PIN = 11; // RX du module
unsigned int TX_PIN = 10; // TX du module
unsigned int RST_PIN = 12;
HTTP http(9600, RX_PIN, TX_PIN, RST_PIN);


// the setup routine runs once when you press reset:
void setup() {
  Serial.begin(9600);
  while(!Serial);
  Serial.println("Starting!");
}

// the loop routine runs over and over again forever:
void loop() {
  // if (shouldTrackTimeEntry()) 
  trackTimeEntry();
}

// functions
void print(const __FlashStringHelper *message, int code = -1){
  if (code != -1){
    Serial.print(message);
    Serial.println(code);
  }
  else {
    Serial.println(message);
  }
}

void trackTimeEntry(){
  char response[32];
  char body[100];
  char auth[50];
  Result result;
  String identifiant = "user_4";
  int temperatureAir = 45;
  int humiditeSol = 23;
  int humiditeAir = 23;
  int phSol = 6;
  int niveauEau = 23;

  print(F("Cofigure bearer: "), http.configureBearer("internet"));
  result = http.connect();
  print(F("HTTP connect: "), result);

  sprintf(body, BODY_FORMAT,identifiant, temperatureAir, humiditeSol, humiditeAir, phSol, niveauEau);
  print(F("body: "), body);

  result = http.post("https://us-central1-MY_PROJET_ID.cloudfunctions.net/insertMesure", body, response);
  print(F("HTTP POST: "), result);

  print(F("HTTP disconnect: "), http.disconnect());

  delay(10000);
}

但是服务器向我发送了400 error code。 我应该如何格式化请求?