Stuck at Install and Setup Mongoose

Hello,
I’ve been quite stumped on this second step for Install and Setup Mongoose,

  • “mongoose” should be connected to a database
    I’ve been unable to solve it for a while.
    What should I do first?
    myApp.js that matters right now:
require('dotenv').config();
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI);

dependencies of package.json:

"dependencies": {
    "body-parser": "^1.15.2",
    "dotenv": "^8.2.0",
    "express": "^4.12.4",
    "mongodb": "^3.3.2",
    "mongoose": "^5.11.15"
  }

.env:
MONGO_URI="^3.3.2"

Links:

Please let me know if I need to add anything else, and respond simply and as soon or as late as you want.

do again the stepts to get the URI, this is not a correct one for sure

Alright, I’ll do that.

also when you ask for help, a link to the challenge would be great

Oh, sorry. I’ll do that now.

and when you find the correct URI, don’t share it, it’s a secret password needed to get to your database

I ran out of credits, so I’m just going to work on other things on the forum for now. Hopefully, this conversation will start again around next month.

Hello again, @ILM. I got myself some more credits, so I started working again. UPDATE: I was able to fix that error, but now able to fix that error, but now a new one shows up:
Error: Invalid schema, expected mongodb or mongodb+srv
What is wrong with my code?
sample.env:
MONGO_URI="mongodb+srv://<db_username>:<db_password>@goofy-goober.y94jf.mongodb.net/"
package.json:

{
  "name": "fcc-mongo-mongoose-challenges",
  "version": "0.0.1",
  "description": "A boilerplate project",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "body-parser": "^1.15.2",
    "dotenv": "^8.2.0",
    "express": "^4.12.4",
    "mongoose": "^5.11.15"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/freeCodeCamp/boilerplate-mongomongoose.git"
  },
  "keywords": [
    "node",
    "mongoose",
    "express"
  ],
  "license": "MIT"
}

myApp.js that matters:

require('dotenv').config();
const mongoose = require('mongoose');
mongoose.connect("process.env.MONGO_URI");

did you create an .env file from this adding the correct URI?

No… It was just there. I’ll make one right now. UPDATE: I made one, yet the error is still there.

you are missing the second argument, check again challenge instructions

Actually, I did add it, I just didn’t know what to do with it.
mongoose.connect("process.env.MONGO_URI", { useNewUrlParser: true, useUnifiedTopology: true });
Sorry for replying so late.

why the first argument has become a literal string?

oh, it was even earlier, my bad to look at code when I should sleep

Good morning @ILM. It’s fine. It happens with everybody, when they’re extremely tired, they see something that’s not real at some point… At least, I hope.

Yes I had trouble with this as well. I solved it by installing Docker and running a local mongodb container on my computer and connecting the web application using this container. This solution will pass.

I did an extensive read on docker documentation to achieve this and wrote a script for myself to help me memorize how I did it.

#!/bin/bash

# NOTE: This script works only for arch-based linux distributions
# Installs necessary dependencies (if needed)
# Starts up a mongoDB server container (if container already exists, removes it and starts it again)

function is_program_installed() {
    # takes a single argument as input - name of program
    # program assumes built-in shell command "command" is present on all distros
    if [[ -x $(command -v $1) ]]; then
        echo true
    else
        echo false
    fi
}

# 1-check if docker installed
has_docker=$(is_program_installed docker)
if [[ "$has_docker" = false ]]; then
    echo Docker not found. Installing Docker with Pacman...
    # check if pacman is installed
    has_pacman=$(is_program_installed pacman)
    if [[ $has_pacman = false ]]; then
        echo "You do not have pacman installed. Install docker manually or install pacman manually and try again."
    else
        sudo pacman -S docker
        #check if user installed docker
        has_docker=$(is_program_installed docker)
        if [[ $has_docker = false ]]; then
            echo Docker not installed. Script will terminate.
        fi
    fi
fi

# 2-install mongodb/mongodb-atlas-local image with docker and start a container
# NOTE: docker will not download image if it's already downloaded and is latest version
user=user
pass=pass
port=27017
started=false
if [[ $has_docker = true ]]; then
    echo Installing image...
    docker pull mongodb/mongodb-atlas-local # this will work even if image is already downloaded
    echo Image installed!
    # 2-a check if a container named 'mongodb-atlas' already exists, if so, stop it then delete it
    docker_regex='^mongodb-atlas$'
    result="$(docker ps -a -f name=$docker_regex)"
    name_regex='[[:space:]]+(mongodb-atlas)'
    [[ $result =~ $name_regex ]]
    if [[ -n "${BASH_REMATCH[1]}" ]]; then
        echo -e "\'mongo-db-atlas\' container found running in Docker.\nStopping then removing the container..."
        docker stop mongodb-atlas
        docker rm mongodb-atlas
    fi
    # 2-b run container with default username and password TODO: add arguments to script to allow custom user, pass and port
    echo Starting container...
    docker run -e MONGODB_INITDB_ROOT_USERNAME=$user -e MONGODB_INITDB_ROOT_PASSWORD=$pass -d --name=mongodb-atlas -p $port:$port mongodb/mongodb-atlas-local
    # i should probably use exit code from docker instead of regex to check if the container successfully started
    result="$(docker ps -f name=$docker_regex)"
    [[ $result =~ $name_regex ]]
    if [[ -z "${BASH_REMATCH[1]}" ]]; then
        echo Failed to start container.
    else
        started=true
        echo -e "Container is running!\nProceeding with creation of .env file..."
    fi
fi
# 3-write .env file which will be read and used by the project
if [[ $started = true ]]; then
    FILE="$(pwd)/.env"
    if [[ ! -e $FILE ]]; then
        touch "$FILE"
        echo -e "MONGO_URI=\"mongodb://$user:$pass@localhost:$port/?DirectConnection=true\"\n" >"$FILE"
        echo -e "File created.\n Finished! You can start using mongodb-atlas server hosted locally within your project!"
    else
        echo "$FILE" file already exists, either delete it or update it manually.
    fi
fi