Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

92
Vistas
How do clean this code up? Node function taking multiple params with Mongoose

I am learning mongoose and I have ran into a "clean code" issue. The code works fine and is connected to another file containing my schema. The issue is the function call for "createNewApplicant."

It is getting messy really quick. I am trying to work towards creating an awesome job application / onboarding website for our talent team at my company.

Does anyone have any clean coding tips to shorten the function calls? I've thought of using an object but then I just pass the problem off to my object constructor and it doesn't really solve the issue.

Thank you so much for the time and consideration.

const mongoose = require('mongoose')
const Applicant = require('./Applicant')
const colors = require('colors')

//connecting to mongodb with mongoose
mongoose.connect(MONGOURI,
    () => {
        console.log('connected'.cyan)
    },
    e => console.error(e)
)

//a constructor function which creates a new applicant and stores it in our db
const createNewApplicant = async (fName, lName, eAddress, pNumber, hobbiesArray, addressObject) => {
    try{


        //phone number validation (checks length of phone number)
        if (pNumber.length != 10){
            console.log("incorrect phone number | shutting down function createNewApplicant".bgRed)
            return
        }

        //we have to await because Applicant.create returns a promise
        const applicant = await Applicant.create({
            firstName: fName,
            lastName: lName,
            emailAddress: eAddress,
            phoneNumber: pNumber,
            hobbies: hobbiesArray,
            address: addressObject
         })
        console.log("New Applicant Saved".yellow)
    } catch (e) {
        console.log(e.message)
    }
}

//our function call to create a new applicant
createNewApplicant(
    "Matthew", 
    "Igloo", 
    "myigloo@gmail.com", 
    "9188585576",
    ["running", "skipping", "jumping"],
    { "city": "Tulsa", "state": "OK" }
)
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

So I would suggest you divide everything into smaller chunks

Eg. create multiple folder/files each doing a dedicated tasks..

  • Models folder contains only the mongoose Models
  • Core folder contains the actual logic to connect to models (like find, create, update & delete etc) - make it generic i.e. eg. create fn should only accept an object and save it without validating or anything
  • Controller folder which takes the input, create a format and send it to core for inserting, finding, update or delete
  • Also separate your validation and use libraries like joi or yup.. you can perform validation directly in route or in controller; I actually create middleware for this job

If you need more help on how to properly structure, you can chat up with me :)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda