Home > OS >  set field value based on other fields mongoose schema
set field value based on other fields mongoose schema

Time:02-19

I am trying to set a value based on the value of two other fields in my schema but im getting the following error

ReferenceError: Cannot access 'isPending' before initialization

here is the model

const mongoose = require('mongoose');

const bookingSchema = new mongoose.Schema({
  userId: {
    type: mongoose.Schema.Types.ObjectId,
    required: true,
    ref: 'User',
  },
  sitterId: {
    type: mongoose.Schema.Types.ObjectId,
    required: true,
    ref: 'Sitter',
  },

  start: {
    type: Date,
    required: true,
  },
  end: {
    type: Date,
    required: true,
    min: this.start,
  },
  accepted: {
    type: Boolean,
    default: false,
  },
  declined: {
    type: Boolean,
    default: false,
  },
  paid: {
    type: Boolean,
    default: false,
  },
  pending: {
    type: Boolean,
    default: true,
    set: isPending,
  },
});

const isPending = (accepted, declined) => {
  !accepted && !declined ? true : false;
};

any ideas? is it possible to do

CodePudding user response:

check this

use this to get access to other fields value:

pending: {
    type: Boolean,
    default: true,
    set: function() { return !this.accepted && !this.declined }
}
  • Related