#Can't access connect to MongoDB through mongoose

80 messages · Page 1 of 1 (latest)

indigo hedge
#

I'm trying to initialise a connection with MongoDB through mongoose (typescript) but it's just not showing any connectiong thingy; but it't not timing out so im lost af what i've done wrong

db connection code:

import mongoose from 'mongoose';

export async function connectToDatabase() {

    const db = (await mongoose.connect(process.env.DB_URI as string, {
        serverSelectionTimeoutMS: 5000,
        
    })).connection;

    db.on('error', console.error.bind(console, 'Connection Error'));

    db.on('open', () => {
        console.log(`Connected to DB - ${db.name}`);
    })

    return db;
}

please help me 😭
I've tried looking through documentation but to no avail. Weirdly enough I set up different users for me and the bot but I'm able to access the db through Robo 3T and the VS code UI but just can't do it through the code itself

rose loom
#

Do you use tsc to compile typescript to js?

indigo hedge
#

nope

#

rn im using ts-node

rose loom
#

Yh, tsc your typescript and run ur compiled JavaScript

indigo hedge
#

im too lazy to compile every time i want to run it

#

aight

rose loom
#

tsc watch then

indigo hedge
#

i use autosave and im not tryna burn this old ahh laptop

#

still the same

rose loom
#

I don’t see any compiled typescript

#

Run the compiled typescript to js

indigo hedge
#

i compiled

#

i just cleared

#

also new update

#

i tried mongoose.createConnection() instead of mongoose.connection().connection

#

and now its actually timing out

#

which im gonna assume is the right step

rose loom
indigo hedge
#

i mean i cleared the terminal

rose loom
#

Ah I’m guessing your javascript is in the bin directory?

indigo hedge
#

yeah

rose loom
#

I’ve always had issues connecting with mongo running bun’s ts or ts-node

indigo hedge
#

huh?

#

ohh

#

damn

rose loom
#

npm ls shows?

#

And if you leave it trying to connect for a very long time does it ever throw an error?

indigo hedge
indigo hedge
#

had it running while i went school and nada

#

when i came back

rose loom
#

I’m really not too sure. What’s your start script for node . ?

#

Also wrap your function in a try catch

indigo hedge
#

which function?

#

connectToDatabase?

#

im getting concerningly close to rewriting everything

rose loom
#

I don’t believe that would help, I’m unsure on what’s happening tbh. Ig if create connection works then use that 🤷‍♂️ you can chain. .asPromise on it

indigo hedge
#

oh i forgot to say

#

create connection didn't work either 😍

rose loom
#

No you did say, mb

#

I’m not too sure in all honesty. Try different versions of mongoose, are you sure your ip is whitelisted or all IPs are?

indigo hedge
#

i put all IPs

rose loom
#

Ur connection string can you share it but remove important stuff

#

Username password

indigo hedge
#

cool

#

mongodb+srv://<username>:<password>@<database>.vo7ra.mongodb.net/?retryWrites=true&w=majority&appName=database

rose loom
#

Does removing the &appName= database fix it

indigo hedge
#

uno momento

#

nope

rose loom
#

No idea then

indigo hedge
#

oh well

#

thanks for trying to help tho

rose loom
#

Utterly cinfused

indigo hedge
#

💀

rose loom
#

If it’s running on ur machine via the other app it’s not some kinda firewall problem

#

You’re using the same url from the app

#

Maybe it’s node being whacky?

#

You sure it’s the latest compiled ts you’re running? If you add logs into the function, like “connecting to db” they all log?

indigo hedge
#

tryin it now

#

yeah

#

ok so now im really confused

#

it suddenly just started working??

#

just not in the way i intended

#

i wrote some code for it to upload some data

#

test daya

#

data

#

and it worked??

indigo hedge
#

yeah its working now

#

idk what i did but it did the right thing ig

#

ty for tryna to help tho @rose loom

indigo hedge
rose loom
#

Maybe some atlas issues? Maybe a ban from too many connections?

#

I’m unsure but glad it’s working