error while retrieving information from firestore through cloud functions - node.js

this is the screenshot of how my firestore database looks.
i am trying to retrieve this information from index.js file in cloud functions
my index.js file looks like :
const functions = require('firebase-functions');
const express = require('express');
const firebase = require('firebase-admin');
const firebaseApp = firebase.initializeApp(
functions.config().firebase
);
const db = firebaseApp.firestore();
const app = express();
app.set('view engine','ejs');
//-------------------------------------------------------------------//
app.get("/home",function(req,res){
db.collection('users').get().then((snapshot)=>{
console.log(snapshot.docs);
});
});
exports.app = functions.https.onRequest(app);
whenn i run the command : firebase serve --only functions,hosting
i am getting this error :
⚠ Your requested "node" version "8" doesn't match your global version "12"
✔ functions: functions emulator started at http://localhost:5001
i functions: Watching "/home/chahat/Desktop/firebase 2.0/functions" for Cloud Functions...
i hosting: Serving hosting files from: public
✔ hosting: Local server: http://localhost:5000
⚠ functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
✔ functions[app]: http function initialized (http://localhost:5001/samsungmap-17515/us-central1/app).
⚠ functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
i functions: Beginning execution of "app"
i functions: Finished "app" in ~1s
i functions: Beginning execution of "app"
⚠ External network resource requested!
- URL: "http://169.254.169.254/computeMetadata/v1/instance"
- Be careful, this may be a production service.
⚠ External network resource requested!
- URL: "http://metadata.google.internal./computeMetadata/v1/instance"
- Be careful, this may be a production service.
> (node:6741) UnhandledPromiseRejectionWarning: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
> at GoogleAuth.getApplicationDefaultAsync (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:160:19)
> at processTicksAndRejections (internal/process/task_queues.js:97:5)
> at async GoogleAuth.getClient (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:502:17)
> at async GrpcClient._getCredentials (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-gax/build/src/grpc.js:92:24)
> at async GrpcClient.createStub (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-gax/build/src/grpc.js:213:23)
> (node:6741) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
> (node:6741) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
⚠ functions: Your function timed out after ~60s. To configure this timeout, see
https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
> /usr/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:619
> throw new Error("Function timed out.");
> ^
>
> Error: Function timed out.
> at Timeout._onTimeout (/usr/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:619:23)
> at listOnTimeout (internal/timers.js:549:17)
> at processTimers (internal/timers.js:492:7)
//------------UPDATE---------------------------//
After using :
app.get("/home",function(req,res){
db.collection('users').get().then((snapshot)=>{
res.json(snapshot.docs)
});
});
rather than my prev home route, i am still getting the following error which i am unable to understand .
ERROR :
chahat#chahat:~/Desktop/firebase 2.0$ firebase serve --only functions,hosting
⚠ Your requested "node" version "8" doesn't match your global version "12"
✔ functions: functions emulator started at http://localhost:5001
i functions: Watching "/home/chahat/Desktop/firebase 2.0/functions" for Cloud Functions...
i hosting: Serving hosting files from: public
✔ hosting: Local server: http://localhost:5000
⚠ functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
✔ functions[app]: http function initialized (http://localhost:5001/samsungmap-17515/us-central1/app).
⚠ functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
i functions: Beginning execution of "app"
i functions: Finished "app" in ~1s
i functions: Beginning execution of "app"
⚠ External network resource requested!
- URL: "http://169.254.169.254/computeMetadata/v1/instance"
- Be careful, this may be a production service.
⚠ External network resource requested!
- URL: "http://metadata.google.internal./computeMetadata/v1/instance"
- Be careful, this may be a production service.
⚠ functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
i functions: Beginning execution of "app"
⚠ External network resource requested!
- URL: "http://169.254.169.254/computeMetadata/v1/instance"
- Be careful, this may be a production service.
⚠ External network resource requested!
- URL: "http://metadata.google.internal./computeMetadata/v1/instance"
- Be careful, this may be a production service.
> (node:16552) UnhandledPromiseRejectionWarning: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
> at GoogleAuth.getApplicationDefaultAsync (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:160:19)
> at processTicksAndRejections (internal/process/task_queues.js:97:5)
> at async GoogleAuth.getClient (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:502:17)
> at async GrpcClient._getCredentials (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-gax/build/src/grpc.js:92:24)
> at async GrpcClient.createStub (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-gax/build/src/grpc.js:213:23)
> (node:16552) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
> (node:16552) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
> (node:16583) UnhandledPromiseRejectionWarning: Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
> at GoogleAuth.getApplicationDefaultAsync (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:160:19)
> at processTicksAndRejections (internal/process/task_queues.js:97:5)
> at async GoogleAuth.getClient (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:502:17)
> at async GrpcClient._getCredentials (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-gax/build/src/grpc.js:92:24)
> at async GrpcClient.createStub (/home/chahat/Desktop/firebase 2.0/functions/node_modules/google-gax/build/src/grpc.js:213:23)
> (node:16583) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
> (node:16583) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
⚠ functions: Your function timed out after ~60s. To configure this timeout, see
https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
> /usr/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:619
> throw new Error("Function timed out.");
> ^
>
> Error: Function timed out.
> at Timeout._onTimeout (/usr/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:619:23)
> at listOnTimeout (internal/timers.js:549:17)
> at processTimers (internal/timers.js:492:7)
please help.
thankyou

As stated in the documentation, you need to send a response to the client in order to terminate the function. If you don't send a response, then the function will time out. If you are trying to send all the documents back, you will need to write something like this:
app.get("/home",function(req,res){
db.collection('users').get().then((snapshot)=>{
res.json(snapshot.docs)
});
});

from my experience, timed out will raise if you do not send the status of the request, whether it's a success or failure.
I'm modified/added your code with return status of your request as success (status: 200)
app.get("/home",function(req,res){
db.collection('users').get().then((snapshot)=>{
res.json(snapshot.docs)
return res.status(200).send(snapshot.docs) // add return status
});
});

Related

(node:1928) UnhandledPromiseRejectionWarning: MongooseError: Operation `users.createIndex()` buffering timed out

Getting the following error when I tried to estalish connection to my MongoDB Atlas.
[nodemon] starting `node index.js`
Note-It backend listening at http://localhost:5000
(node:1928) UnhandledPromiseRejectionWarning: MongooseError: Operation `users.createIndex()` buffering timed out after 10000ms
at Timeout.<anonymous> (C:\Users\Meet\OneDrive\Desktop\note-it-backend-api\backend\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:149:23)
at listOnTimeout (internal/timers.js:557:17)
at processTimers (internal/timers.js:500:7)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:1928) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1928) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
***********************Connected to MongoDB successfully***********************
I cant make any HTTP requests because of this
The db.js contents are :
const mongoose = require('mongoose');
// Connection string for MongoDB ATLAS
const mongoURL = "mongodb+srv://new-meeti:<password>#meet-dev.cc0qw.mongodb.net/?retryWrites=true&w=majority"
const connectToMongo = () => {
// .connect method taks 2 args, 2nd one is a callback function
mongoose.connect(mongoURL, () => {
console.log("***********************Connected to MongoDB successfully***********************");
})
}
module.exports = connectToMongo;
This was more than a year old project hosted on heroku but then I had to shift to to some other hosting service so again deploying is getting problematic due to various deprecation issues I think.
A help would be really appreciated. Thanks :)
PS : I checked my Network access on the MongoDB Atlas and 0.0.0.0/0 (includes your current IP address) is active.

NestJS - UnhandledPromiseRejectionWarning

Short version:
I am looking for a way to way to run my NestJS application with the '--trace-warnings' flags that nodeJS offers. Is there some way to do this or does NestJS offer something similar?
Long version:
Hi! NestJS noob here. I am trying to run a dev version of the NestJS application I am working on. However, on starting the application I get the error below.
Clearly, it is missing a catcherror somewhere! However, the dev version has a LOT of updates and this error can be anywhere so I am hoping for a more efficient way of finding this bug than just checking every single new function! In the errormessage there are a few tips on flags to run while starting the application (node --trace-warnings ...). However, these are for node and not NestJS.
So therefore my question; is there some way to run NestJS with the --trace-warnings flag or some other efficient way to find where I am missing the catcherror?
Thanks in advance!
Error:
(node:72899) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:72899) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:72899) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:72899) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
(node:72899) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
Well, 6379 is the Redis default port, it seems like Nest cannot connect to it :)
By the way, give a look at this paragraph:
https://docs.nestjs.com/exception-filters#catch-everything
In order to catch every unhandled exception (regardless of the
exception type), leave the #Catch() decorator's parameter list empty,
e.g., #Catch().
import {
ExceptionFilter,
Catch,
ArgumentsHost,
HttpException,
HttpStatus,
} from '#nestjs/common';
#Catch()
export class AllExceptionsFilter implements ExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const request = ctx.getRequest();
const status =
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
response.status(status).json({
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
});
}
}
To answer your specific question:
I am looking for a way to way to run my NestJS application with the '--trace-warnings' flags that nodeJS offers. Is there some way to do this or does NestJS offer something similar?
I've been trying to do the same thing, and there seems to be next-to-no documentation online on how to do this. Through some trial and error, I've found that this seems to work:
node --trace-warnings -r source-map-support/register --inspect dist/main
Note that this won't rebuild your application (neither when you first start it up, or by watching for changes), so you'll need to do that manually. The debugger is enabled via --inspect switch, remove that if you don't need to debug.
try to use a npm response / request logger like winston or moesif you will get a lot of help trust me.
here are the links for these library for nestjs
https://github.com/scalio/nest-couchdb

Self hosting the APIM Developer portal: Unable to publish locally using "npm run publish"

Cloned the repository and followed all the steps mentioned in the documentation but i am unable to publish locally using "npm run publish" folder. The publish is working fine in others' machines but only i have the issue in my machine.
Expected behavior:
Code should be published to "dist/website" folder
Actual Behavior:
Getting below error on running "npm run publish" command.
Error:
(node:12516) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.
(node:12516) _UnhandledPromiseRejectionWarning: TypeError: invalid options argument
at optsArg (C:\Users\MAGALI\repos\Dev portal new\api-management-developer-portal-master\dist\publisher\index.js:136663:11)
at mkdirp (C:\Users\MAGALI\repos\Dev portal new\api-management-developer-portal-master\dist\publisher\index.js:136454:10)
at C:\Users\MAGALI\repos\Dev portal new\api-management-developer-portal-master\dist\publisher\index.js:183119:13
at new Promise ()
at FileSystemBlobStorage.uploadBlob (C:\Users\MAGALI\repos\Dev portal new\api-management-developer-portal-master\dist\publisher\index.js:183117:16)
at LocalStyleBuilder. (C:\Users\MAGALI\repos\Dev portal new\api-management-developer-portal-master\dist\publisher\index.js:37575:42)
at Generator.next ()
at C:\Users\MAGALI\repos\Dev portal new\api-management-developer-portal-master\dist\publisher\index.js:37560:71
at new Promise ()
at module.exports../node_modules/#paperbits/core/publishing/localStyleBuilder.ts._awaiter (C:\Users\MAGALI\repos\Dev portal new\api-management-developer-portal-master\dist\publisher\index.js:37556:12)
(node:12516) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 11)
(node:12516) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Puppeteer cluster sometimes cannot start in k8s

I'm using puppeteer with puppeteer-cluster, this is deployed on k8s and everything works great.
The only problem I'm having is that sometimes the pod won't start, and throws this exception:
(node:24) UnhandledPromiseRejectionWarning: Error: Unable to launch browser, error message: read ECONNRESET
at Cluster.<anonymous> (/app/node_modules/puppeteer-cluster/dist/Cluster.js:107:23)
at Generator.throw (<anonymous>)
at rejected (/app/node_modules/puppeteer-cluster/dist/Cluster.js:6:65)
at process._tickCallback (internal/process/next_tick.js:68:7)
followed by:
(node:24) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
and:
(node:24) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
This doesn't affect me functionally considerably, as k8s just creates a new pod (which works fine), but I'd like to understand what happens, and if I can fix it.
Cluster initialization code:
const cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_CONTEXT,
maxConcurrency: Constants.CONFIG.URL_CONCURRENCY,
retryLimit: Constants.CONFIG.CLUSTER_INTERNAL_RETRIES,
timeout: Constants.CONFIG.MAX_TIMEOUT
puppeteerOptions: { ignoreHTTPSErrors: true, args: ["--no-sandbox"] }
});
versions:
"puppeteer": "^2.0.0",
"puppeteer-cluster": "^0.18.0",

Firestore local http with real db: The Cloud Firestore emulator is not running so database operations will fail with a 'default credentials' error

I want to run a firebase http cloud function locally accessing a real remote firestore database.
To do it, I'm running my functions locally using:
FIREBASE_CONFIG="/path-to-credential.json" GOOGLE_APPLICATION_CREDENTIALS="/path-to-credential.json" firebase emulators:start --only functions
As written here https://firebase.google.com/docs/admin/setup, this should permit the initialization of the admin sdk without any parameter. My functions index.js is as following
import * as admin from 'firebase-admin';
import * as fromHttpFunc from './http/func';
admin.initializeApp();
export const httpFunc = fromHttpFunc.httpFunc;
When I then call the function in the browser, this is what I get in logs
[warn] ⚠ The Cloud Firestore emulator is not running so database operations will fail with a 'default credentials' error.
[debug] [2019-07-20T13:16:28.656Z] [runtime-status] Ephemeral server survived. {}
[info] > (node:41939) UnhandledPromiseRejectionWarning: Error: The incoming JSON object does not contain a client_email field
[info] > at JWT.fromJSON (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-auth-library/build/src/auth/jwtclient.js:165:19)
[info] > at GoogleAuth.fromJSON (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:294:16)
[info] > at GoogleAuth.getClient (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:476:52)
[info] > at GrpcClient._getCredentials (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-gax/build/src/grpc.js:107:40)
[info] > at GrpcClient.createStub (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-gax/build/src/grpc.js:223:34)
[info] > at new FirestoreClient (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/#google-cloud/firestore/build/src/v1/firestore_client.js:128:39)
[info] > at ClientPool.Firestore._clientPool.pool_1.ClientPool [as clientFactory] (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/#google-cloud/firestore/build/src/index.js:315:26)
[info] > at ClientPool.acquire (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/#google-cloud/firestore/build/src/pool.js:61:35)
[info] > at ClientPool.run (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/#google-cloud/firestore/build/src/pool.js:114:29)
[info] > at Firestore.readStream (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/#google-cloud/firestore/build/src/index.js:995:26)
[info] > (node:41939) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
[info] > (node:41939) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[warn] ⚠ Your function timed out after ~60s. To configure this timeout, see
https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
[warn] ⚠ Your function timed out after ~60s. To configure this timeout, see
https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
I don't get what's wrong, I also checked if the path to the credential is ok by doing cat /path-to-credential.json and I have a json with the client_email field.
Managed to have the local cloud functions working with the real remote DB by making sure of the following:
run firebase login and login into the project you want to use
follow this comment https://github.com/firebase/firebase-functions/issues/121#issuecomment-337065268 to make sure the file is not there (I had a invalid_grant error)
run export GOOGLE_APPLICATION_CREDENTIALS="SERVICE_ACCOUNT_PATH.json" in the terminal session you want to use (replace SERVICE_ACCOUNT_PATH)
serve your http functions NOT by using firebase emulators:start --only functions but using firebase serve --only functions. Currently, this is an undocumented behaviour: https://github.com/firebase/firebase-tools/issues/1412#issuecomment-504561828.
initialize the app as following (update YOUR_APP_ID):
admin.initializeApp({
credential: admin.credential.applicationDefault()
});

Resources