Why am I not able query ( User.findOne() ) my mongoDB after setting up a Schema?

147 Views Asked by At

I can't seem to query my MongoDB after setting up the MongoDB schema. I don't understand where I am going wrong with my MongoDB schema, any help is appreciated!

I WAS earlier successful in querying my mongodb before creating a schema, using queries like this:

const uri = process.env.MONGODB_URI; 
const client = new MongoClient(uri);

const result = await client.db("inride_adverts").collection("adverts").findOne({OrgEmail: OrgEmailToSignIn}); 

However, according to a YouTube tutorial am following (10:40 mins), after setting up a mongodb schema, I am NOT successful in using the following query to interact with my mongodb:

User.findOne( {OrgEmail: signUpEmail} ).exec();

Find below my simple User Schema:

./models/User.js

import { mongoose} from 'mongoose';

const UserSchema = new mongoose.Schema({
    OrgName: {
        type: String,
        required: true
    },

    OrgEmail: {
        type: String,
        required: true
    },

    OrgPwd: {
        type: String,
        required: true
    }

}, { collection: 'adverts' });

export const User = mongoose.model('User', UserSchema);

Also, find below my server.js file

./server.js

import express from 'express';
import { User } from './models/User.js';
import mongodb from 'mongodb';
import { mongoose} from 'mongoose';
mongoose.connect(db, { useNewUrlParser: true })

mongoose.connect(db, { useNewUrlParser: true })    
    .then( () => console.log("MongoDB Connected..." ))
    .catch(error => console.log(err))
   
app.route('/advertiserRegister')

    .post( async (req, res) => {
        let formData = req.body;
        let signUpName = formData.signUpName;
        let signUpEmail = formData.signUpEmail;
        let signUpPwd = formData.signUpPwd; 

        console.log("signUpName: " +signUpName);
        console.log("signUpEmail: " +signUpEmail);
        console.log("signUpPwd: " +signUpPwd);
        
        if(signUpPwd !== signUpPwdConfirm){
            console.log("Passwords arent EQUAL!");
            return 0;
        } else {

            try{
                console.log("signUpEmail>>> : " + signUpEmail ); 

                // Validation passed!
                const testing = await User.findOne( {OrgEmail: signUpEmail} ).exec();
                
                console.log("testing >>> : " ,testing ); 

                res.redirect('/advertiserLogin')
    
            } catch {
                //console.error('error', code);
                console.log("Error logging in ");
                res.redirect('/advertiserRegister')

            };
        }
    });

The server.js file yields:

MongoDB Connected...
signUpName: Merc Enterprise LTD
signUpEmail: [email protected]
signUpPwd: 555

signUpEmail>>> : [email protected]
testing >>> : **null** 
Error logging in
1

There are 1 best solutions below

0
SirBT On

Turns out that the reason I was NOT able to query my collection was due to the fact that I was unknowingly querying the incorrect collection being: adverts under the incorrect database being: inride_adverts.

I came to understand and realise that In my ./models/User.js, the mongoose.model('User', UserSchema); code creates a new database in Atlas MongoDB called test and creates a totally new collection called User.

Having understood this, I am able to populate and query this collection successfully!