I'm having trouble retrieving data from a collection in my MongoDB backend. Every time I try, I encounter an error stating that the client is not defined. Has anyone else experienced this issue and knows how to resolve it?
Error: Client is not defined. const db = client.db('products');
const express = require('express');
const app = express();
const morgan = require('morgan');
const bodyParser = require('body-parser');
const user = require("./routes/user");
const cors = require('cors');
const MongoClient = require('mongodb').MongoClient;
const mongo_uri = 'mongodb+srv://***:***@cluster0.fetfl.gcp.mongodb.net/*****?retryWrites=true&w=majority';
const db = client.db('products')
const collection = db.collection('brand')
MongoClient.connect(mongo_uri, { useNewUrlParser: true })
.then(client => {
console.log('connected');
const db = client.db('products');
const collection = db.collection('brand');
app.listen(port, () => console.info(`REST API running on port ${port}`));
}).catch(error => console.error(error));
app.get('/', (req, res) => {
db.collection('brand').find().toArray().then(results => {
console.log(results)
})
.catch(error => console.error(error))
})