将 firestore 与 firebase 功能相结合
Combining firestore with firebase functions
我不知道如何将 firestore 与 firebase 函数结合起来我想检查文档中的某些字段和 return 一些数据,如果字段等于某事。
我的代码:
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")
});
我的错误:
可能会出什么问题?如何将 firestore 与函数正确结合?
所以我自己解决了这个问题 :) 如果有人遇到类似问题,请在下面输入代码
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"}'
*/
我不知道如何将 firestore 与 firebase 函数结合起来我想检查文档中的某些字段和 return 一些数据,如果字段等于某事。
我的代码:
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")
});
我的错误:
可能会出什么问题?如何将 firestore 与函数正确结合?
所以我自己解决了这个问题 :) 如果有人遇到类似问题,请在下面输入代码
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"}'
*/