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

223
Vistas
How to pass value from controller to mongoose model in Node.js?

I have this in controller section

const isRequired =await credentialWhichRequireLogin(portId)

    let createdCredential

    if (isRequired) {
         createdCredential = await containerCredentialQuery.create({
            userId: _id,
            portUsername,
            portPassword,
            portId,
        });
    } else {
         createdCredential = await containerCredentialQuery.create({
            userId: _id,
            portId,
        });
    }

and this is mongoose model:-

    portUsername: {
        ...optionalString,required:true 
    },
    portPassword: {
        ...optionalString,required:true

    },
    portId: {
       ...requiredMongooseObjectId,ref:'Port'
    },
    userId: {
        ...requiredMongooseObjectId,ref:'auth'
    }
},{timestamps:true});

in controller section if isRequired is true i want mongoose model portUsername required to be true and if false i want to be false i.e.dynamic...how could i achieve dynamic mongoose schema type based on controller type...if isRequired is true model will required all 4 value but if false it accept only 2 value and restrict taking portUsername and portPassword

about 4 years ago · Juan Pablo Isaza
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