Cannot find module 'charset' - firebase

I new in this and I working on firebase and postman but when I try to run my test code to test server I got this error
const functions = require("firebase-functions");
// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
exports.helloWorld = functions.https.onRequest((request, response) => {
functions.logger.info("Hello logs!", {structuredData: true});
response.send('Hello world');
});
this is my code that i run for test

Related

Why am I keep getting INTERNAL ERROR 500 with my firebase cloud function?

I was trying to deploy a cloud function on firebase but I keep getting this error. I deleted all of my logic to console the response and debug but nothing's changed.
I am sure that the problem is not related to permissions because the invocation is allowed for unauthenticated users.
this is the block of my function:
// Firebase config
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const cors = require("cors")({
origin: true
});
admin.initializeApp();
exports.emailMessage = functions.https.onCall((req, res) => {
return cors(req, res, async() => {
console.log(req);
console.log(res);
}).catch(() => {
res.status(500).send("error");
});
});

Firebase Serve: Error occurred while parsing your function triggers

Im trying to use firebase serve in order to get my localhost url
I can't seem to move past this issue. Here is my index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
// Database reference
const dbRef = admin.firestore().doc('tokens/demo');
// Twitter API init
const TwitterApi = require('twitter-api-v2').default;
const twitterClient = new TwitterApi({
clientId: 'YOUR_CLIENT_ID',
clientSecret: 'YOUR_CLIENT_SECRET',
});
// STEP 1 - Auth URL
exports.auth = functions.https.onRequest((request, response) => {});
// STEP 2 - Verify callback code, store access_token
exports.callback = functions.https.onRequest((request, response) => {});
// STEP 3 - Refresh tokens and post tweets
exports.tweet = functions.https.onRequest((request, response) => {});

Cloud Firestore emulator not running when using firebase serve method

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.

Cloud Functions Firestore Trigger is not getting triggered

I am facing an issue with my firestore cloud functions. I am trying to set up a trigger to send a notification when a new document is added.
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
/*exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello ninjas!");
});*/
// Function to be called when new event occurs
const createNotification = notification => {
return admin
.firestore()
.collection("notifications")
.add(notification)
.then(doc => console.log("Notification added", doc));
};
//Trigger when new project is created
exports.projectCreated = functions.firestore
.document("project/{projectId}")
.onCreate(doc => {
const project = doc.data();
const notification = {
content: "Added a new project",
time: admin.firestore.FieldValue.serverTimestamp()
};
return createNotification(notification);
});
On the client side when I add a new project I am able to see the console message that notification is added but when I check the logs in the Cloud function I cannot see any logs. Am I missing anything here?
Replace
.onCreate(doc => {
with
.onCreate((snap, context) => {
as described at
https://firebase.google.com/docs/functions/beta-v1-diff#cloud-firestore

How to post trigger firebase cloud functions with express.js

What i am trying to do is send data(inputs) from client to firebase cloud function with post trigger using express.js and use that data to create a new user.
this is the cloud function:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const cors = require('cors')({origin: true});
admin.initializeApp(functions.config().firebase);
exports.registeration = functions.https.onRequest((req, res) => {
res.status(400).send({
message: 'error',
message2:req,
message3:req.body
});
res.send({
message: 'success'
});
});
So how do i write client part with express.js
Maybe something like this :
const express = require('express');
// Create the server
const app = express();
app.use((err, req, res, _next) => {
console.log('Error handler', err);
if(err){
res.status(400).send({
message:'error',
message2:req,
message3:req.body
});
} else {
res.send({
message: 'success'
});
}
});
exports.registeration = functions.https.onRequest(app);

Resources