I've had this problem for quite a while now since we got a new router, so I thought I might just ask here: I cannot connect to MongoDB via mongoose and other applications without a VPN and it always gives me this error: https://pastebin.com/5fDVk5C0. This is my code for connecting:
mongoose.connect(config.mongodb.database).then(() => {console.log("Backend successfully connected to MongoDB");
async function createTokens() {
if (
!(await tokens.findOne({
accessTokens: { $exists: true },
refreshTokens: { $exists: true },
clientTokens: { $exists: true },
}))
) {
await tokens.create({});
} else {
var jwtTokens = await tokens.findOne({
accessTokens: { $exists: true },
refreshTokens: { $exists: true },
clientTokens: { $exists: true },
});
for (var i in jwtTokens) {
if (Array.isArray(jwtTokens[i])) {
for (var x in jwtTokens[i]) {
try {
await jwtTokens.updateOne({ $pull: { [`${i}`]: null } });
let object = jwtTokens[i][x];
let decodedToken = jwt.decode(object.token.split("eg1~")[1]);
if (
DateAddHours(
new Date(decodedToken.creation_date),
decodedToken.hours_expire
).getTime() <= new Date().getTime()
) {
await jwtTokens.updateOne({ [`${i}.${x}`]: [] });
await jwtTokens.updateOne({ $pull: { [`${i}`]: [] } });
}
} catch { }
}
}
}
}
function DateAddHours(pdate, number) {
var date = pdate;
date.setHours(date.getHours() + number);
return date;
}
}
createTokens();
})
.catch((error) => {
console.error("Error connecting to MongoDB: ", error);
});```
It seems like that it's trying to connect via ipv6 in the error, which shouldn't be a problem is my router supports that. I have also had this problem for other programs such as NPM or the EpicGames friends list, they just all do not work without a VPN.
"What did you try and what were you expecting?": I would expect Mongoose to connect normally in my ExpressJS app instead of it giving me a connection error. I know it's not the problem of me whitelisting my IP because the IP of my VPN is not Whitelisted either and it's only in mongoose (Compass works).