0

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: hundredcent.a@gmail.com
signUpPwd: 555

signUpEmail>>> : hundredcent.a@gmail.com
testing >>> : **null** 
Error logging in
SirBT
  • 1,580
  • 5
  • 22
  • 51
  • Your original query uses the `adverts` collection, your Mongoose query [uses the `users` collection](https://mongoosejs.com/docs/guide.html#collection). – robertklep Aug 15 '22 at 13:25
  • @robertklep Kindly explain how i can correct this in code – SirBT Aug 15 '22 at 14:09
  • Kindly look at the link I posted in my comment which explains how to set the collection for a schema. – robertklep Aug 15 '22 at 14:16
  • @robertklep Thanks. I corrected the code by adding the collection option to the schema. You'd think this would direct the query inside the "adverts" collection right? But `await User.find( {OrgEmail: signUpEmail} ).exec()` doesn't work? – SirBT Aug 15 '22 at 16:29
  • It looks okay now. Try turning on [debugging](https://mongoosejs.com/docs/faq.html#enable_debugging) to see what Mongoose does exactly when it makes the query. – robertklep Aug 15 '22 at 16:43
  • @robertklep Thanks for the response. Turns out that the reason I was NOT able to query my collection was due to the fact that I was unknowingly querying the wrong/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 – SirBT Aug 17 '22 at 07:26

1 Answers1

0

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!

SirBT
  • 1,580
  • 5
  • 22
  • 51