mongoDB, setting of required attribute of a field by async function

27 views Asked by At

I run a web app on fastAPI framework in react and next.js. I'm trying to set the value of 'required' attribute of a field by calling an async function like below. This is a part of a schema.

reportType: {
      type: Schema.Types.ObjectId,
      ref: 'ReportType',
      required: async function () {
        try {
          if (!this.populated('megaType')) await this.populate('megaType');
          if (this.megaType.type === '보고서') {
            console.log('repoprt type result: ', this.megaType.type === '보고서');
            return true;
          } else {
            console.log('repoprt type result: ', this.megaType.type === '보고서');
            return false;
          }
        } catch (error) {
          console.error('Error in populating megaType:', error);
          return false;
        }
      },
    },
    slideType: {
      type: Schema.Types.ObjectId,
      ref: 'SlideType',
      required: async function () {
        try {
          if (!this.populated('megaType')) await this.populate('megaType');
          if (this.megaType.type === '보고서') {
            console.log('slide type result: ', this.megaType.type === '보고서');
            return true;
          } else {
            console.log('repoprt type result: ', this.megaType.type === '보고서');
            return false;
          }
        } catch (error) {
          console.error('Error in populating megaType:', error);
          return false;
        }
      },
    },

mongood version is "mongoose": "^7.3.1" In log messages, definitely it shows like this.

repoprt type result:  false
repoprt type result:  false

but mongoDB doesn't seem to set required with the return value of the function. It shows like this.

Error: MegaPrompt validation failed: slideType: Path `slideType` is required., reportType: Path `reportType` is required.
    at ValidationError.inspect (C:\genai\tokaireport\node_modules\mongoose\lib\error\validation.js:50:26)
    at formatValue (node:internal/util/inspect:805:19)
    at inspect (node:internal/util/inspect:364:10)
    at formatWithOptionsInternal (node:internal/util/inspect:2279:40)
    at formatWithOptions (node:internal/util/inspect:2141:10)
    at console.value (node:internal/console/constructor:352:14)
    at console.warn (node:internal/console/constructor:385:61)
    at handler (webpack-internal:///(api)/./pages/api/v1/report/prompt-management/mega-prompt/index.js:542:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  errors: {
    slideType: ValidatorError: Path `slideType` is required.
        at validate (C:\genai\tokaireport\node_modules\mongoose\lib\schematype.js:1349:13)
        at SchemaType.doValidate (C:\genai\tokaireport\node_modules\mongoose\lib\schematype.js:1333:7)
        at C:\genai\tokaireport\node_modules\mongoose\lib\document.js:2932:18
        at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
      properties: [Object],
      kind: 'required',
      path: 'slideType',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    },
    reportType: ValidatorError: Path `reportType` is required.
        at validate (C:\genai\tokaireport\node_modules\mongoose\lib\schematype.js:1349:13)
        at SchemaType.doValidate (C:\genai\tokaireport\node_modules\mongoose\lib\schematype.js:1333:7)
        at C:\genai\tokaireport\node_modules\mongoose\lib\document.js:2932:18
        at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
      properties: [Object],
      kind: 'required',
      path: 'reportType',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    }
  },
  _message: 'MegaPrompt validation failed'
}
2023-12-08 15:25:09 [error] An error occurred: MegaPrompt validation failed: slideType: Path `slideType` is required., reportType: Path `reportType` is required.
repoprt type result:  false
repoprt type result:  false

What should I do to set the value of 'required' attribute of a field correctly?

I want to set the value of required attribute of a field with the result of the function.

0

There are 0 answers