I’m working on the URL shortener problem and I think I’m having trouble getting MongoDB to work, which is weird as I took all the code from a previous problem and I’m not getting errors in the log.
The following query causes my program to hang up and time out:
Url.find({},function(err, data) {
console.log(“first”);
if (err)
res.send(err);
console.log(“number of data” + data.length);
res.json(data); })
}
Url.find is causing the hangup. It makes me question whether the DB is connected, but there are no errors.
toArray() is a mongoDB method and it is not necessary for a find query if you are using mongoose, which looks like you are. However there isn’t much information provided to debug your problem
Are you connected to the data base?
If so do you have a collection called Url in your db with at least 1 document in it
What does url schema look like ?
etc, etc… usually, these types of problems are easier to troubleshoot if you provide your glitch link
And it’s 0, which means it’s disconnected. I guess we should start there.
in server.js:
var express = require(‘express’);
var mongodb = require(‘mongodb’);
const mongoose = require(‘mongoose’);
mongoose.connect(process.env.MONGO_URI,{useNewUrlParser: true});
var db = mongoose.connection;
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.
See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.
Ok, your first problem is that you are using an incompatible version of mongoDB for mongoDB Atlas, this link says it requires MongoDB [3.4 / 3.6 / 4.0] .
Since you are using mongoose 4.5.7, which uses MongodDB < 3.4, you can not connect. Meaning, you need a minimum of mongoose 5.x to use mongoDB Atlas.
So go to your package.json and use 5.x for mongoose, you can also delete the mongoDB package as mongoose wraps a mongoDB driver with it and is therefore redundant to have both. You should now be able to connect, You have some issues with your code too, but you first need to connect…
Fyi, slight clarification after further reading that may be helpful for other users, actually only MongoDB 4.0 is supported for the free tier clusters, 3.4 , 3.6 are only for the paid clusters, so the correct mongoose version is > 5.2. I’m updating the above link accordingly.