0

I'm reviewing some old code I wrote in Express/Mongo/Mongoose (based on an online tutorial) and can't locate the portion of code which dictates which Collection in MongoDB gets written to.

I have a Database, UsersDB and within that database there are several Collections. The Collection that keeps growing every time the code executes user.save is the users Collection. But I can't find any reference in my code base to the users collection.

The only place in my code where I save a User is:


var app = require('../app');
var util = require('util');
var User = require('../models/user'),
    Auth = User.Auth,
    Name = User.Name,
    Email= User.Email,
    Phone = User.Phone,
    Address = User.Address,
    Company = User.Company,
    PersonalData = User.PersonalData,
    Id = User.Id,
    Photo = User.Photo,
    Member = User.Member,
    CreditCard = User.CreditCard,
    UserObj = User.User;

var moment = require('moment');

var async = require('async');

. . . 
. . . 

exports.user_create_post = [
    (req,res, next) => {
        console.log("Request: " + util.inspect(req.body));
    },
    //VALIDATE
    body('mainEmail', 'Must be valid email.').isLength({min: 5}).trim(),

    //SANITIZE
    sanitizeBody('*').escape(),
 
    //POPULATE NEW DOCUMENT
    (req,res,next) => {
        const errors = validationResult(req);

        var auth = new Auth(
                dateEffective: {value: moment(Date.now()).format("YYYY-MM-DD hh:mm:ss SSS"), attr: {hidden: true, label: ""}},
                username: {"value": req.body.username, "attr": {hidden: false, label: "Username: "}},
                password: {"value": req.body.password, "attr": {hidden: false, label: "Password: "}},
                mainEmail: {"value": req.body.mainEmail, "attr": {hidden: false, label: "Email: "}}
            });
        var user = new UserObj(
            {authData: [auth]}
        );
        if (!errors.isEmpty()) {
            const errorFormatter = ({ location, msg, param, value, nestedErrors }) => {
                // Build your resulting errors however you want! String, object, whatever - it works!
                return `${location}[${param}]: ${msg}`;
              };
              const result = validationResult(req).formatWith(errorFormatter);
              if (!result.isEmpty()) {
                return res.json({ errors: result.array() });
              }
            }
            else {
                user.save(function(err){
                    if (err) { return next(err);}
                });
                res.redirect("http://localhost:4200/two-fa/"+user._id);
        }
    } 

I also have a Models module (user.js):


. . . 
. . . 
. . . 

module.exports = {
    Auth: mongoose.model('Auth', AuthSchema),
    Name: mongoose.model('Name', NameSchema),
    Email: mongoose.model('Email', EmailSchema),
    Phone: mongoose.model('Phone', PhoneSchema),
    Address: mongoose.model('Address', AddressSchema),
    Company: mongoose.model('Company', CompanySchema),
    PersonalData: mongoose.model('PersonalData', PersonalDataSchema),
    Id: mongoose.model('Id', IdSchema),
    Photo: mongoose.model('Photo', PhoneSchema),
    Member: mongoose.model('Member', MemberSchema),
    CreditCard: mongoose.model('CreditCard', CreditCardSchema),
    User: mongoose.model('User', UserSchema)
}

I did a search on my entire code, and nowhere is there any mention of users, which is the Collection that's getting written to.

Where should I look to try to trace how the users collection is getting written to?

halfer
  • 19,824
  • 17
  • 99
  • 186
Crowdpleasr
  • 3,574
  • 4
  • 21
  • 37
  • I just saw that if a 3rd argument to mongoose.model is not provided, then Mongo automatically "pluralizes" the model name to give the name of the Collection. So I guess that answers my question! – Crowdpleasr Sep 18 '19 at 04:09

1 Answers1

0

As per how-to-access-a-preexisting-collection-with-mongoose, if a 3rd argument to mongoose.model is not provided, then Mongo automatically "pluralizes" the model name to give the name of the Collection.

Crowdpleasr
  • 3,574
  • 4
  • 21
  • 37