It work with local DB, but when i created MongoDB Atlas it stoped. Anything wrong with my code?
also it keep creating a db called ‘test".
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const uri = 'mongodb+srv://User:[email protected]/?retryWrites=true&w=majority';
// Connect to the database
mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
console.log('Connected to MongoDB successfully!');
})
.catch((err) => {
console.error('Failed to connect to MongoDB:', err);
});
// Define a schema for the quotes collection
const quoteSchema = new mongoose.Schema({
text: {type: String, required: true},
author: {type: String, required: true}
});
// Create a model for the quotes collection
const Quote = mongoose.model('Quote', quoteSchema);
// This sets up a route that sends the index.html file to the client when they request the root URL.
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
// Serve static files from the "public" directory
app.use(express.static('public'));
// Define an endpoint that returns a random quote from the database
app.get('/api/quotes/random', (req, res) => {
Quote.countDocuments().exec()
.then(count => {
const random = Math.floor(Math.random() * count);
Quote.findOne().skip(random).exec()
.then(quote => {
res.json({text: quote.text, author: quote.author});
})
.catch(err => res.status(500).json({ error: err.message }));
})
.catch(err => res.status(500).json({ error: err.message }));
});
// Define the port to listen on
const PORT = 3000;
// Start the Express app listening on the defined port
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});
[enter image description here](https://i.stack.imgur.com/Q7g6S.png)
i tried everything at this point that i know
2
Answers
Regarding the issue with a database called ‘test’ being created, it’s likely because the
useNewUrlParser
option is set to true. This option automatically creates a database called ‘test’ if one doesn’t already exist. To avoid this, you can setuseNewUrlParser
to false:When connecting to MongoDB the connection string should include the database name. If it does not, the database name ‘test’ will be used.
So perhaps something like: