I have written the following code, it's for a discord bot. When I call the command I get matchID in console for the first time. But when I call the command again I dont get any output. It gets stuck near the point where I have console.log("Stuck Here"). I new to mongoose so I don't know what to do.
if (mongoose.connection.readyState === 0) {
mongoose.connect(`mongodb://localhost/${server}`, {
useNewUrlParser: true
});
console.log('mongoose readyState is ' + mongoose.connection.readyState);
}
console.log("Stuck here!");
mongoose.connection.on("error", function (err) {
console.log("Could not connect to mongo server!");
return console.log(err);
});
mongoose.connection.on('connected', function (ref) {
console.log('Connected to mongo server.');
mongoose.connection.db.listCollections({
name: "matches"
}).next(function (err, collinfo) {
if (err) console.log(err);
if (collinfo) {
Matches.findOne({}, {}, {
sort: {
'created_at': -1
}
}, function (err, match) {
if (err) console.log(err);
console.log(`${match.matchID}`);
})
} else {
}
});
})