I have no clue how to combine firestore with firebase functions i want to check some fields in document and return some data if field equals sth.
My code:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const { getFirestore } = require('firebase-admin/firestore');
const { initializeApp } = require('firebase-admin/app');
const { collection, getDoc, query, where, addDoc } = require("firebase-admin/firestore");
const firebaseConfig = {
...
};
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const roomsRef = db.collection("rooms");
exports.test = functions.https.onRequest((request, response) => {
var { uid, name, password } = request.body;
var q = query(roomsRef, where("creator_uid", "==", uid), where("name", "==", name));
if (q)
var rooms = getDoc(q);
if (rooms.exist)
response.send(rooms.data().name)
else
response.send("nie zadziala xD")
});
My error:
What possibly could go wrong? how to combine firestore with functions properly?
So i solved that on my own :) code below if someone have simillar problem
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
exports.test = functions.https.onRequest((req, res) => {
const { uid, name, password } = req.body;
const nameRef = admin.firestore()
.collection('rooms').where("name", "==", name);
const uidRef = nameRef.where("creator_uid", "==", uid);
const passwordRef = uidRef.where("password", "==", password);
passwordRef.get().then((doc) => {
if (doc) {
res.json(doc.docs.map((docx) => { return docx.data() }));
}
else {
res.json([]);
}
}).catch(err => {
//Internal server error
console.log("Error", err)
res.json(err);
});
});
/* EXAMPLE POST
curl -X POST https://us-central1-test-36302.cloudfunctions.net/xxx
-H 'Content-Type: application/json'
-d '{"name":"xxxa","uid": "B2Xidd3Vw1PL9Kyt5ERFXCjniuF3",
"password": "xxxx"}'
*/