Why will my server code with the MongoDB extension work locally on my computer but not on the DigitalOcean server?

December 3, 2019 151 views
MongoDB Ubuntu Node.js

Hi,

I have made an Authentication API with Node.js and MongoDB which works perfectly on my local machine. As soon as I upload it to Digital Ocean with Ubuntu, however, everything that doesn’t rely on MongoDB works (for example, I have some validation code that checks if the send email is actually an email or not and if it isn’t I get the appropriate response back so the nod.js code is working without errors.) However, if I send details that require to use the MongoDB part of the code, nothing happens. I send the request through Postman and it just stays on ‘Sending request’. When I connect to the database, it prints out “Database connected” so it connects to the database correctly, just won’t do any of the queries I ask it to do.

Am I missing something or any steps I haven’t gone through? I haven’t got a firewall on the Ubuntu Server either so anything should go through it.

1 comment
  • The first line of code that queries the database is this:

    const emailExist = await User.findOne({email: req.body.email});
    if(emailExist) return res.status(400).send(‘Email already connected to an account’);

    I realized the 'await’ function is probably the one that’s preventing the code from proceeding and causing postman to stay on the 'Sending request’. So it’s definitely the database being queried that is a problem, not allowing the code to proceed.

1 Answer

Hello,

This indeed sounds like a database problem, what I could suggest is to make sure that your database details are correctly specified in your node db.js file - check the port, the host, the user and the password and make sure that they are correct.

Also I would recommend taking a look at this article which would show you how to configure MongoDB with your Node application:

https://www.digitalocean.com/community/tutorials/how-to-integrate-mongodb-with-your-node-application

Let me know how it goes!
Regards,
Bobby

by Kathleen Juell
In the process of working with Node.js, you may find yourself developing a project that stores and queries data. This tutorial will show you how to integrate MongoDB with an existing Node application. This process will involve adding user and database information to your application code and using the Object Document Mapper Mongoose. At the end of the tutorial, you will have a working application that will take a user's input and display it in the browser.
Have another answer? Share your knowledge.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!