Reputation: 25
I want to post data to my Firestore database using Firestore cloud functions. here is my code in 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);
});
When I use Postman to test, sending the data:
[{
"identifiant": "identifianttt",
"temperature": 35,
"phSol": 7
}]
The helloWorldJS function returns: identifiant : undefined And The addMesure function returns : Error: could not handle the request
Thank you for your help.
Upvotes: 1
Views: 845
Reputation: 25
Thank you everyone everything works well, I changed the previous code into:
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);
// });
});
I want to send the data with an Arduino using the SIM800L module with the following code:
#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);
}
But the server sends me a 400 error code
.
How should I format the request please?
Upvotes: 0
Reputation: 83103
For the first problem, you should double check that you correctly send the body.
For the second Cloud Function, (after having solved the first problem with the body) you should adapt your code to wait that the promise returned by the asynchronous push()
method resolves before sending back the response, as follows:
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 => {....})
});
Finally, I would suggest you watch the 3 videos about "JavaScript Promises" from the Firebase video series: https://firebase.google.com/docs/functions/video-series/. The first one covers exactly your case (asynchronous call to a Firebase db and then sending back the response)
Upvotes: 1