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

Viewed 73

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

0 Answers
Related