0

I am trying to use use hyper terminal to connect to my mongodb database (created with mongoose) and when I use node app.js to try to connect to the database using hyper terminal, my terminal just freezes and return no response until I press ctrl c to exit and return to the previous line.

My mongoose code looks like the following:

const mongoose = require('mongoose');

mongoose.connect('mongodb://127.0.0.1:27017/fruitsDB');

Please note. I have already connected to the mongodb server by using mongodin the terminal and I have also cd to the directory of my folder where the app.js is located on my pc before running the node app.js on the terminal

screenshort of my terminal hyerterminal code screen

Please I need your help. Thank you

3
  • If you already connected to the db via terminal, I see no problem here. You terminal "did not freeze" Your code just dosnt generate any output. What did you expect to happen? Commented Apr 14, 2022 at 13:15
  • the mangoose command line 'mongoose.connect('mongodb://127.0.0.1:27017/fruitsDB')' is suppose to create a new database 'fruitsDB' into my mongodb database. running a code 'show dbs' in the command line is suppose to show that the database is among the existing database if the code works as you suggested, but nothing of such which means the code did not work in the first place. Commented Apr 14, 2022 at 14:25
  • Add event listener for "connect" & "error" and show if the are triggerd. Not sure if the db is only created when you do something in it, like crate collections/documents. Just because you specifiy in the connection string the database does not mean the database is created on a connection attempt. Commented Apr 14, 2022 at 14:47

1 Answer 1

1

I believe that your terminal is NOT frozen, it is just that there is no console output in your code. You are connecting to Mongoose and then doing nothing after that.

Here are some suggestions,

You can use a callback to check if the connection is connected,

mongoose.connect('mongodb://127.0.0.1:27017/fruitsDB').then(
  () => { 
     console.log("Connected to DB!");
 },
  err => { 
    console.log(err);
 }
);

Add a few event handlers to check if your connection is working,

mongoose.connection.on('open', function(){
  console.log("Connection to Mongo DB is open!");
});

If you want to check for errors during "connect", you can chain an error handler in catch block.

mongoose.connect('mongodb://localhost:27017/test').
  catch(error => console.log(error));

If you want to use Async/Await for the same error handling, then follow the below.

(async () => {
    try {
      await mongoose.connect('mongodb://localhost:27017/test');
      console.log("Successfully connected to Mongo DB");
    } catch (error) {
      console.log(error);
    }
})();

If you want to catch errors happening after the connection is connected to Mongo DB,

mongoose.connection.on('error', err => {
  console.log(error);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank so much for this help. I figured this work exactly but it was nice how you simplified the whole thing. I really do appreciate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.