Incomplete response received from application in Node Js and Mongoose - node.js

I installed my api rest in my hosting using cPanel. The routes work perfect and the db is connected. The problem is when I need to use any mongoose method, i.e. model.find({}), the response is
Incomplete response received from application
For other routes, that don't return any data from the DB, works perfect, using json format.

You cannot run MongoDB on shared hosting. Please refer to this thread..
You can use free cloud service like Heroku https://heroku.com or more sophisticated ones like AWS or Azure. If shared hosting deployment is a must, then an option is use external MongoDB instance. Easiest way to get MongoDB instance is using MongoDB Atlas. There is a free sandbox for development purpose.
To create an instance, follow these steps:
Go to https://www.mongodb.com/cloud/atlas, and login/create account
Click 'build a cluster'. Set it as tier-0 for free instance.
Once the cluster is created, click on connect, then choose 'connect your application'
Copy the mongoDB URI and paste it to your code containing something like mongoose.connect(mongoDBAtlasURIhere, { useNewUrlParser: true, useUnifiedTopology: true})
Example of complete tutorial for Node JS: https://medium.com/#sergio13prez/connecting-to-mongodb-atlas-d1381f184369
Hope this helps.

Related

Can't connect to mongodb container when i add path to a database

I’m running mongoDB as a docker container locally, and I initiate a database creation when starting the container. But I can’t seem to connect to the database from my nodejs application when I add path to the dB. For example i can connect when I use “mongoDB://admin:password#localhost:27017” but not when I use “mongoDB://admin:password#localhost:27017/myDB”.
I'm running the container with docker-compose. Also when I log the output of running the mongo container I see a failure message that says:
"result":"UserNotFound: Could not find user \"admin\" for db \"journalDB\""}}
JournalDB is the name of the db i want to connect to, and i want to be able to connect to it from my nodejs application like so:
mongoose
.connect("mongoDB://admin:password#localhost:27017/journalDB", {
useNewUrlParser: true,
useUnifiedTopology: true,
})
I have read the answer to a similar question here and this documentation , but I feel they have only explained the possible problem, but have not helped me fix it.
I would really appreciate it if there's a straightforward solution.
Maybe try
.connect("mongoDB://admin:password#localhost:27017/journalDB?authSource=admin" ...

Can't authenticate to local MongoDB database

I recently started working on a project with Express and since I'm using Node.js as backend I chose MongoDB as my database. It's my first time working with Mongo but I can't authenticate with Express, it works fine from terminal. I followed the guide from MogoDB blog here. I tried using their Atlas service where I had no problem authenticating. I'm using MongoDB driver. Here's how my connection URI looks like:
const uri = `mongodb://${username}:${password}#127.0.0.1/cloud?retryWrites=true&w=majority`;
I tried changing mongodb:// to mongodb+srv:// but that resulted in invalid connection string error.
You need to specify the authentication database, usually admin:
const uri = `mongodb://${username}:${password}#127.0.0.1/cloud?authSource=admin&retryWrites=true&w=majority`;
If you don't specify it then in your case MonogDB defaults the authentication database to cloud - which is most likely wrong.
When you are accessing mongodb on the web you can click on connect and on connect your application. You will show the uri to copy paste starting with
mongodb+srv://USERNAME:PASSWORD#CLUSTER/DATABASE
You forgot to specify the CLUSTER. Currently is your local Database

expressJS exporting other things in addition to app

In my web application (I'm using expressJS), there are many services (such as mongoDB connection, MQTT connection, etc.) that need to be executed once the whole application is executed (using npm start command). Therefore, I can make use of these services in my entire application. For example, I want to use my MQTT connection in different files.
My idea is to export the MQTT connection, MongoDB connection, etc. in addition to the app this way:
//app.js
module.exports = {
app: app,
mqttConnection: myMQTTConnection,
db: myMongoDB
};
However, we know that this approach doesn't work (I tested it and got an error saying: TypeError: app.set is not a function).
How can I export other things in addition to app from app.js file?
If my approach is not possible, what other approaches can I use? (considering the fact that many services (such as connecting to a server, etc.) are asynchronous)

MongoLab and Heroku without mongoose

I am currently working with a team in a NodeJS project that uses express and a local MongoDB database.
We did not implement mongoose into the project, and now we are at a stage where we wish to upload the project using heroku, so that we can have a central point as we further develop different parts of the project (apps and web pages that consume the data from the database.)
We managed to upload the project to heroku, and some of the pages are working, but we can't access the database, it returns "500 - internal server error" everytime we try to access data from the MongoDB.
After some research we could only find that it's best to use MongoLAB to store the data in a cloud based datacenter, but we can't seem to find ways to connect our project with MongoLab without the use of mongoose, which we didn't install to the project.
My question is: is there a way to connect our NodeJS project to MongoLab using only the original MongoDB npm module, without having to install and use mongoose? We are at a stage that refactoring our DB-access classes, and implement mongoose, would cause our project's deadline to be delayed.
Thank you for your time.
You can definitely use mongoDB with mLab(previously mongoLab), without using mongoose.
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://ID:PASSWORD!#SUBDOMAIN.mlab.com:PORT/DATABASE_NAME', function(err, db) {
console.log("Connected correctly to server.");
db.close();
});

Link mongoDB instance database to meteor local

I've recently setup an ec-2 instance to deploy a meteor application on AWS. My app works correctly with mongoDB database.
Now I want to connect my meteor project's database to the EC2 database and then I've tried some command lines :
start my meteor project with a new mongo url with the following command line : "MONGO_URL="mongodb://username:password#xx.xx.xxx.xxx:27017/Tasks" meteor" but it returns the following screen
connect to mongo with the other following command line :
"meteor mongo --url xx.xx.xxx.xxx:27017" but it returns a timeout. I thought it was due to the lack of username and password however when I add this option meteor does not assume the command.
connect to mongo in my js collection file with
"export const Tasks = mongoose.connect("mongodb://username:password#xx.xx.xxx.xxx:27017/Tasks").connection;" but it returns "Error: connect ECONNREFUSED" (second screen)
Is there any tips to manage to establish database connection from local meteor ?
Thank you for answer,
Martin
If the database and the Meteor processes are in the same machine, you can call it from "localhost".
MONGO_URL="mongodb://user:password#localhost/Tasks"
If you really want to use the IP + Port approach, you have to ensure that it's accessible from the EC2 Security Groups you're using.
The same applies for connecting manually in Mongoose but you should't be using Mongoose with Meteor, it has it's own way of handling collections. If you need something more powerful, or a way to extend it yourself checkout the Meteor Simple Schema project.

Resources