Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!