mongoDB and glitch (URL shortener)

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.

Any pointers where to start? Much appreciated!

Non an expert on MongoDB, but quick google search suggests:

Url.find({}).toArray(function(err, data) {
  console.log('first');
  if (err) res.send(err);
  console.log('number of data' + data.length);
  res.json(data);
});

Both don’t work unfortunately. I added .toArray and no dice. Also there’s no invalid syntax, my posting was just cut off when I posted.

Any other ideas? What’s interesting is that none of the console.log posts.

1 Like

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

  1. Are you connected to the data base?
  2. If so do you have a collection called Url in your db with at least 1 document in it
  3. What does url schema look like ?

etc, etc… usually, these types of problems are easier to troubleshoot if you provide your glitch link

I added

console.log(mongoose.connection.readyState);

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;

In package.json:

"dependencies": {
	"express": "^5.0.0-alpha.2",
	"mongodb": "^2.2.5",
	"mongoose": "^4.5.7",
	"cors": "^2.8.1",
	"body-parser": "^1.15.2"

}
in dot.env:

MONGO_URI=mongodb+srv://avillanu8:EDITED_OUT@cluster0-htav7.mongodb.net/test

Interesting, so when I try to dig for more data by using:

const options = {
  useNewUrlParser: true,
}

mongoose.connect(process.env.MONGO_URI, options, function(err) {
    if (err) {
        console.log(err);
    } else {
        console.log('Connected');
    }
});

I get:

Error: Invalid mongodb uri “mongodb+srv://avillanu8:asdfasdf@cluster0-htav7.mongodb.net/test”. Must begin with “mongodb://”

Wanted to add to that if I used mongoose.connect(process.env.MONGO_URI,{useNewUrlParser: true}); I get no error.

@32blaine

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.

Note: Backticks are not single quotes.

markdown_Forums

Take a look here, MongoDB and Mongoose - Install and Set Up Mongoose and also post your glitch link

HI Dereje1

I read through that link, yes I did that and all (IP Address whitelist, etc.)

Here is the glitch link:

https://glitch.com/~frequent-tractor

Thanks

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…

1 Like

Wow thank you so much!

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.

Thanks! I had exactly same problem