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:
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.
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