I've been trying to implement some Cloud Functions that get and post data from Cloud Firestore(I'm using postman). I want to test these locally, however I cannot get the Cloud Firestore emulator to run.
My index.js file
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const express = require("express");
admin.initializeApp();
const app = express();
app.get("/posts", (req, res) => {
admin
.firestore()
.collection("posts")
.orderBy("createdAt", "desc")
.get()
.then((data) => {
let posts = [];
data.forEach((doc) => {
posts.push({
postId: doc.id,
body: doc.data().body,
userHandle: doc.data().userHandle,
createdAt: doc.data().createdAt,
});
});
return res.json(posts);
})
.catch((err) => console.log(err));
});
app.post("/post", (req, res) => {
const newPost = {
body: req.body.body,
userHandle: req.body.userHandle,
createdAt: new Date().toISOString(),
};
admin
.firestore()
.collection("posts")
.add(newPost)
.then((doc) => {
res.json({ message: `document ${doc.id} created successfully` });
})
.catch((err) => {
res.status(500).json({ error: "something went wrong" });
console.log(err);
});
});
exports.api = functions.https.onRequest(app);
I can get data when never i use firebase deploy method using postman. result for firebase deploy method is response (status:200) [in postman]
However if i try firebase serve or firebase serve --only function . i get like this...
firebase serve
=== Serving from 'C:\Users\Yuvan M\Documents\Visual Studio Code\React\meme-zha\meeme-functions'...
! Your requested "node" version "8" doesn't match your global version "12"
i functions: Watching "C:\Users\Yuvan M\Documents\Visual Studio Code\React\meme-zha\meeme-functions\functions" for Cloud Functions...
+ functions[api]: http function initialized (http://localhost:5000/meeme-zha/us-central1/api)
in above code i can't get response like
i functions : preparing emulate function.
if i use this local address http://localhost:5000/meeme-zha/us-central1/api , this gives me the error like this...
! functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
! External network resource requested!
I use firebase emulator:start - not working.I tried every solution that posted on internet for this kind of problem, also i followed the documentation . still i can't solve it.