MongoDB and Mongoose Challenge

I am doing it On glitch and I am getting an error in my logs that is ->

connection error: Error: No hostname or hostnames provided in connection string

what does this mean ? It is pointing out the line 5 in my code

// MY CODE //

var express = require('express');
var app = express();
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect(process.env.MONGO_URI) //line 5

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'))
db.once('open', function() {
  console.log('Connected to MongoDB')
})

//Create a model
var personSchema = new Schema({
  name: {type: String, required: true },
  age: Number,
  favoriteFoods: [String]
}) 

var Person = mongoose.model('Person', personSchema);

//Create and save a model

var createAndSavePerson = function(done) {
  
  var person = new Person({name: 'Priyanka', age: 22, favoriteFoods: ["bread", "butter"]})
  person.save((err,data)=> (err)? done(err):done(null,data))
};

please help?

I’m guessing that the problem is here:

mongoose.connect(process.env.MONGO_URI) //line 5

Have you confirmed that that environment variable is set up? Can you do:

console.log('mongo uri =', process.env.MONGO_URI);

It depends on your environment, but you may have to do some setup to get the string in the env variables. I use a package named dotenv when I’m developing, but glitch may handle those differently.

Putting things like this in the env variables is always a good idea, but maybe you could just use the string to test that it works.

1 Like

Check your .env file in the file tree. You will need to have MONGO_URI set to be equal to the link to access your mLab db. The .env is a shell file so there cannot be spaces on either side of the = sign, e.g. MONGO_URI=address , NOT MONGO_URI = address

1 Like

Thankyou cz i was doing this mistake …

@kevinSmith do uh have any idea what this error mean:-

Error: cannot find module ‘express’ .

first i dnt get it that why we need ‘express’ here and even if i include express module by adding this - var express = require(“express”);
var app = express();

then olso i am getting same error .

  1. what is an ELIFECYCLE err mean?

do tell if you have any idea?

ELIFECYCLE means End of Life Cycle. This causes due to unexpected stop of the program execution. This may occur for several reasons. I think there may be some error in your script.

You may try to replace the direct URL of your mlab or localhost & try.

mongoose.connect(process.env.MONGO_URI) //line 5

That sounds to me like it can’t find express. Make sure that it is in your package.json. From the command line you can do an “npm install express --save” (or “yarn add express” if you’re using yarn). If it’s already in your package.json, you can run “npm install” (“yarn install”) to make sure everything is up to date and installed. If you think something is wrong, you can delete your .node_modules directory and redo “npm install” (“yarn install”).

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