#having trouble passing env var to mongodb driver

23 messages ยท Page 1 of 1 (latest)

rapid elk
#

hello everyone ๐Ÿ™‚ noob here

how come i get this error message from the mongodb driver's Connect() function

i have this code:

        // this Println() works fine
    fmt.Println(os.Getenv(MONGO_URI_ENV_VAR))

    client, err := mongo.Connect(  // this is where it's erroring out 
        context.TODO(),
        options.Client().ApplyURI(os.Getenv(MONGO_URI_ENV_VAR)),
    )
    if err != nil {
        log.Fatalf("mongo.Connect(): %s\n", err)
    }

and i get the printline of the api key from the env var fine
but the mongo.Connect() function errors out saying:

2023/07/22 21:47:25 mongo.Connect(): error parsing uri: scheme must be "mongodb" or "mongodb+srv"

the url looks something like this btw:

"mongodb+srv://<username>:<password>@<project-name>.mongodb.net/?retryWrites=true&w=majority"
hazy smelt
rapid elk
#

yes

dapper blade
#

And you did replace your mongodb username and password with the replacers in the url

#

<username>:<password>@<project-name>

rapid elk
#

yes i mean if i just change argument i pass to ApplyURI() to just a regular string of the uri it works, but i'd rather not commit that to the repo, nomsayin

marsh stag
rapid elk
#

not that i can see, the output of

    fmt.Fprintf(os.Stdout, "%q\n", os.Getenv(MONGO_URI_ENV_VAR))

is:

"`mongodb+srv://<username>:<password>@<project-name>.mongodb.net/?retryWrites=true&w=majority`"

with those extra singlequotes wrapped

marsh stag
#

whats up with that ` surrounding the string

rapid elk
#

is it not supposed to do that?

marsh stag
#

no

rapid elk
#

hmmm coz my env var is this:

MONGO_URI='mongodb+srv://<username>:<password>@<project-name>.mongodb.net/?retryWrites=true&w=majority'
#

isn't that how you do it?

#

now quotes if i do a regular fmt.Println()

marsh stag
#

the backticks are getting included as part of the string

#

try removing them

rapid elk
#

it's single quotes not backticks if i remove them wouldn't my shell parse out the reserved characters?

marsh stag
#

the string you sent above is backquotes

#

i dont see any single quotes

rapid elk
#

actually hold on i could have sworn i've tried this before seems like it's working wihout the quotes

marsh stag
#

but if it were single quotes the shell would correctly omit them from the env itself

rapid elk
marsh stag
#

so its working now?