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))
};
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.
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 , NOTMONGO_URI = address
@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();
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.
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.