#MongoDB FindOne
34 messages · Page 1 of 1 (latest)
what error are you getting from the fetchdoc function?
here is the doc schema:
its just a goroutine im getting back telling me what line it errors out on
print the error instead and see what it actually says
panic is not really for error handling anyway
yup
hmmm...says "client is disconnected"
is there something I should be doing when creatign the mongo client first and then passing that into another function?
I think the defer that you have there is disconnecting the client
ok lemme try again
maybe I need to take that line out in my mongoclient func?
here's my new InitMongoClient function:
uri := "mongodb+srv://<username>:<password>@cluster0.ifbxo.mongodb.net/myFirstDatabase?retryWrites=true&w=majority"
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
if err != nil {
panic(err)
}
return client
and it returned nothing back to me
instead of panicing in the db init function you could return (*Client, error) and handle error in your main function
how do you return 2 things in a function?
idk if you have to set serverAPI explicitly for the connection, although I am not a mongodb enjoyer
return client, nil on success
or return nil, err in your case
and
func InitMongoClient() *mongo.Client
should be
func InitMongoClient() (*mongo.Client, error)
if you haven't done the tour (https://go.dev/tour) that's a good starting point
ok leme try
it doesn't look like anything is wrong with it:
mongoClient, err := InitMongoClient()
fmt.Printf("%v and %v", mongoClient, err)
do I need to use the UseSession function on the mongo Client?
you have to check the error and go on from there
if err != nil { ... }
but yeah the error seems empty so the problem is somewhere else
problem is that nothing is being returned back to me when I print the error, but it's not returning any mongo data back from me as well