Database

Heroku : Application error : error code=H12 desc=”Request timeout” : when using remote database

Posted on Updated on

I was working with a Node application I built, which connected to a hosted MongoDB database. Everything worked perfectly on my development machine. Once I pushed everything to heroku is where I would start getting problems.

I received this error after trying to connect to my apps webpage

Application error

An error occurred in the application and your page could not be served. If you are the application owner, check your logs for details. You can do this from the Heroku CLI with the command
heroku logs --tail

When checking the logs this error stood out

2019-08-01T18:36:14.968797+00:00 heroku[router]: at=error code=H12 desc=”Request timeout” method=GET path=”/api/notes” host=spongebobs-basin
-47509e646.herokuapp.com request_id=e70f60dc-321d-421c-9c24-6974ba3a79bb fwd=”99.99.99.9″ dyno=web.1 connect=0ms service=30000ms status=503 b
ytes=0 protocol=https

So what are the important things in this line?

  • desc=”Request timeout”
  • method=GET path=”/api/notes”
  • service=30000ms
  • status=503 b

I am connecting to the correct path, using the correct method, but after 30 seconds the server is timing out , so heroku sends a 503 Service Unavailable.

This was a strange one to figure out since locally everything worked great. I changed some things around for testing and found that the application on heroku was not connecting to the remote mongoDB host. Locally it worked great. Was this a herkoku problem?

Solution:

The .env file I used locally required the mongoDB URI to be in quotes. The app would not work without the quotes. But the configuration variables in heroku needed to be added without the quotes.

  • .env  ‘mongodb+srv://username:password@somecluster.mongodb.net/note-app’
  • Config Vars  mongodb+srv://username:password@somecluster.mongodb.net/note-app

Converting Rails from sqlite3 to PostgreSQL

Posted on

Your mod 3 and 4 projects were probably done using sqlite3. If you did your mod5 project in sqlite3, tsk tsk!

  • edit your Gemfile
  • comment out sqlite and add gem pg
  • Screen Shot 2018-06-13 at 11.51.49 AM
  • run “bundle install”
  • edit config/database.yml
  • Change your databases names, as below.
  • Screen Shot 2018-06-13 at 12.01.58 PM
  • run rake db:create
  • run rake db:migrate
  • run rake db:seed(if you have a seed file)
  • Now test your app, and you are now using Postgres!

How do I know that it worked?

  • Go to the little Postgres elephant, and click him
  • Press stop
  • Screen Shot 2018-06-13 at 12.11.01 PM
  • If your site does not work, then you migrated successfully. Refresh your browser if needed
  • Now start up Postgres so you can continue developing!

Drop a database from postgres

Posted on Updated on

in bash

type psql

\list or \l to list your database

terminate any connections by using the following command. example if databse named YourDatabase

select pg_terminate_backend(pid) from pg_stat_activity where datname=’YourDatabase’;

*use procid instead of pid for older versions of postgres

DROP DATABASE “YourDatabase”;

Now your database is gone.

https://stackoverflow.com/questions/7073773/drop-postgresql-database-through-command-line for reference

MONGODB: What is a projection?

Posted on Updated on

A projection is when you search for documents in MongoDB but only get back the fields that you request.

example records   {name: “Tom”, age: “35”, zipcode: “10003”,_id:778789754}

using projection for name and zipcode: {name: “Tom”,  zipcode: “10003”}

 

var mongo = require(“mongodb”).MongoClient;
var url = “mongodb://localhost/addressbook”;
var collectionName = “friends”;
mongo.connect(url,function(err,db){
if(err) console.log(err)//throw err;
else{
var collection = db.collection(collectionName);
collection.find({},{name: 1, age: 0,_id:0,zipcode: 0}).toArray( function(err,documents){
if(err) console.log(err)//throw err;
else{
console.log(documents)
}
});

db.close();
}
})