Thu Aug 10 2023

Fetch MongoDB Data

Node JS232 views
Fetch MongoDB Data

File Name: user-model.js

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
    fstName: String,
    lstName: String,
    email: String
});

const User = mongoose.model('User', userSchema);

module.exports = User;

File Name: user-controller.js

const User = require('./user.model');

const getUserList = async () => {
    try {
        const users = await User.find();
        console.log('User List: ', users);
    } catch(e) {
        throw new Error(e);
    }
}

module.exports = {getUserList}

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.