Can't return all my data from db in mongodb and node

This is my code snippet, I want to return all the data on the Products collection but I can’t get the data but when I use x.findOne() method to query the db, I get the last data in the collection. Please help me out on this.

app.get('/api/willsolar/products/getall', async(req, res) => {
        try {
            await productDataBase.find({}, (err, products) => {
                if(err) {
                    console.log(err)
                } else {
                    console.log(products);
                    // console.log(products);
                    // please I need help on how to figure out the problem of get request for all items here
                   // this only consoles the last item on the collection, but I want all to be consolled.
                    return res.json({msg: 'found items'});
                }
            })
        } catch (error) {
            console.log(error)
        }
    });

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Thank you, but do you have any knowledge on the issue I am trying to resolve?

This is my server connection

'use strict';

const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const bodyParser = require('body-parser');
const session = require('express-session');
const passport = require('passport');
const ObjectID = require('mongodb').ObjectID; //finding mongodb id with search
const LocalStrategy = require('passport-local');
const bcrypt = require('bcrypt');

// import password checker to server side for password checking
const passwdChecker = require('./auth/passwordChecker'); //take this file to auth file later

// import database configuration file here
const CONNECTION_URI = require('./config_files/dbConfig');
// import database connection file here
const myDB = require('./config_files/connection');
// import session secret variable
const SESSION_SECRET = require('./config_files/sessionSecret');

// import user schema
const User = require('./models/user');
// import auth routes
// import routes here
const routes = require('./routes/routes');
// const routes = require('./routes/routes');
const auth = require('./middleware/auth');

// console.log(SESSION_SECRET());

const app = express();
// set port number here
const PORT = process.env.PORT || 5000


// set the cors from interferring in the app
app.use(cors());
app.use(express.json({limit: '30mb', extended: true}));
app.use(express.urlencoded({limit: '30mb', extended: false}));


// set express app to use session
app.use(session({
    secret: SESSION_SECRET(),
    resave: true,
    saveUninitialized: true,
    cookie: { secure: false }
}));


// initialize passport for auth
app.use(passport.initialize());
app.use(passport.session());


// connect to database here
myDB(async (client) => {
    const userDataBase = await client.db('will-solar').collection('Users');
    const productDataBase = await client.db('will-solar').collection('Products');

    // instatiate routes here
    routes(app, userDataBase, productDataBase);
    auth(app, userDataBase);
})
.then(() => {
    app.listen(PORT, () => {
        console.log(`Server running on PORT ${PORT}....\nConnected to DataBase successfully!`);
    })
})
.catch((e) => {
    // modify here later for json response
    console.log(e);
    throw new Error("something bad happened");
});

Still stuck, someone please help me out.

Hello there,

It sounds like you are using find on bulk op, as opposed to this method find on collection

Otherwise, your code seems fine. Do you have a live example you could share showing this behaviour? A minimal example.

Also, what version of mongodb are you using?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.