I recently began using google cloud and I wanted to host my Postgres database to google cloud and deployed my node js application to app engine however I am having issues connecting my nodejs app to the hosted database,
I have already enabled the Cloud SQL API.
I use node-postgres here is my connection object:
const { Pool } = require("pg");
const pool = new Pool({
user: process.env.DB_USER,
password: process.env.DB_PASS,
port: 5432,
host: `/cloudsql/${process.env.CLOUD_SQL_CONNECTION_NAME}`,
database: process.env.DB_NAME,
});
module.exports = pool;
This is the query:
router.get("/", async (req, res) => {
try {
const { rows } = db.query("SELECT * FROM users ORDER BY id DESC");
res.json(rows);
} catch (err) {
console.error(err);
}
});
this is my app.yaml:
runtime: nodejs14
env_variables:
DB_USER: postgres
DB_PASS: password here
DB_NAME: the db name here
CLOUD_SQL_CONNECTION_NAME: my connection name here
beta_settings:
cloud_sql_instances: my connection name here
when I run this I get an ENONET error:
UnhandledPromiseRejectionWarning: Error: connect ENOENT /cloudsql/instance_id/.s.PGSQL.5432
at PipeConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:121478) 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:121478) [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.
I am not sure what exactly happened, I followed the official documentation but it still didnt succeed.
I think that your problem is that in your connection object, the field "host" expects an IP address and it looks like you're putting in the UNIX socket name mentioned in some pages of the documentation. Try to put in the valid public or private IP address of your Cloud SQL instance instead as that's what the library you're using expects in this case.
Also, make sure that you configure your App Engine correctly in each case(public or private IP) like in this documentation page
Related
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.
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
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
});
});
I have been following an Udemy course to learn mongodb, and since the tutor uses a cloud based ide (cloud9), I have also decided to use the same. However, since cloud9 now requires AWS account I thought of using CodeSandbox. But I can't figure out how to start Mongo server on it.
After installing both Mongod and MongoDB, if I try to run the app.js, it gives me error:
(node:1297) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on firstconnect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (/sandbox/node_modules/mongodb/lib/core/topologies/server.js:433:11)
at Pool.emit (events.js:198:13)
at createConnection (/sandbox/node_modules/mongodb/lib/core/connection/pool.js:577:14)
at connect (/sandbox/node_modules/mongodb/lib/core/connection/pool.js:1007:11)
at makeConnection (/sandbox/node_modules/mongodb/lib/core/connection/connect.js:31:7)
at callback (/sandbox/node_modules/mongodb/lib/core/connection/connect.js:247:5)
at Socket.err (/sandbox/node_modules/mongodb/lib/core/connection/connect.js:276:7)
at Object.onceWrapper (events.js:286:20)
at Socket.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
(node:1297) UnhandledPromiseRejectionWarning: Unhandled promise rejection. Thiserror originated either by throwing inside of an async function without a catchblock, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1297) [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.
If I try to run mongod, it says
mongod: command not found
This is my app.js
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/yelp_camp");
I have tried installing mongo and mongod using codesandbox's UI , using terminal (npm install), and doing what cloud9's docs say (I know it should not work but I can't find any Codesandbox documentation)
Is it possible to do this in codesandbox or will I have to use MongoAtlas?
Thanks in advance!
I think there's no database hosting on CodeSandbox, so you should use an external service.
Some services you can use:
MLab (Free plan)
ObjectRocket (Free trial)
Compose (Free trial)
MongoAtlas
MLab example
Set up a database at MLab
Copy and paste the connection string
A simple example function would be:
let initMongo = async () => {
try {
await mongoose.connect(
"mongodb://<dbuser>:<dbpassword>#ds123456.mlab.com:49878/testdatabase",
{
useNewUrlParser: true,
useUnifiedTopology: true
}
);
return console.log("Database connected!");
} catch (e) {
return console.log("Database error!", e.message);
}
};
Here's the documentation of CodeSandbox.
CodeSandbox has a starter for connecting to hosted MongoDB like Atlas - see https://codesandbox.io/s/mongodb-database-example-starter-v3ker
I have been following along to Acadamind's NodeJS / Express / MongoDb-build a shopping cart tutorial on youtube and everything was going fine until I restarted the mongodb server when I wanted to update a product item.
Before this change, I would write npm start and the code would run smoothly, but now I am given a response of:
(node:49784) UnhandledPromiseRejectionWarning: Error: connect
ECONNREFUSED 127.0.0.1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
(node:49784) 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)
(node:49784) [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.
I am new to node.js and am not sure why this is occuring when I believe all i did was change a product item in my seed folder.
Here is the link to my github repository:clone my git hub "shopping-cart" repository here
Note: since this is my first node.js project that I am having problems with, is this the correct format to ask for help due to the larger nature or the project.
Thanks for any feedback!!!
Mongod needs to be running before starting the app:
sudo service mongod start
Status check:
sudo service mongod status
(should show Active (running))