Edit nested schemas

I would like to know how I can make it so that I can edit vehicles, invoices, from this type of schema:

{
  'mobile': String,
  'customerData': {
    city: String,
    province: String,
    street: String,
    postalCode: String,
    labourRate: String,
    notes: String,
    status: Boolean,
    vehicles: [{
      driverName: String,
      plateNumber: String,
      vin: String,
      make: String,
      model: String,
      color: String,
      body: String,
      transmission: String,
      engine: String,
      year: String,
      ac: Boolean,
      invoices: [{
        oilChange: Number,
        date: Date,
        number: String,
        mileage: String,
        discount: Number,
        repairDescription: String,
        totalAmount: Number,
      }],
    }],
  },
  'ownerData': {
    garage: mongoose.Schema.Types.ObjectId,
  },
  'email': String,
  'lastName': String,
  'password': String,
  'role': String,
  'firstName': String,
}

So far, I’m able to edit the customer information, but what about when it’s a few levels deep?

This is my forest/users.js file:

collection('users', {
  actions: [],
  fields: [{
    field: 'city',
    type: 'String',
    get: (user) => {
      return user.customerData.city;
    },
    set: ((user, value) => {
      if (!user.customerData) {
        user.customerData = {};
      }
      user.customerData.city = value;
    })
  }, {
    field: 'province',
    type: 'String',
    get: (user) => {
      return user.customerData.province;
    },
    set: ((user, value) => {
      if (!user.customerData) {
        user.customerData = {};
      }
      user.customerData.province = value;
    })
  }, {
    field: 'street',
    type: 'String',
    get: (user) => {
      return user.customerData.street;
    },
    set: ((user, value) => {
      if (!user.customerData) {
        user.customerData = {};
      }
      user.customerData.street = value;
    })
  }, {
    field: 'postalCode',
    type: 'String',
    get: (user) => {
      return user.customerData.postalCode;
    },
    set: ((user, value) => {
      if (!user.customerData) {
        user.customerData = {};
      }
      user.customerData.postalCode = value;
    })
  }, {
    field: 'labourRate',
    type: 'String',
    get: (user) => {
      return user.customerData.labourRate;
    },
    set: ((user, value) => {
      if (!user.customerData) {
        user.customerData = {};
      }
      user.customerData.labourRate = value;
    })
  }, {
    field: 'notes',
    type: 'String',
    get: (user) => {
      return user.customerData.notes;
    },
    set: ((user, value) => {
      if (!user.customerData) {
        user.customerData = {};
      }
      user.customerData.notes = value;
    })
  }, {
    field: 'status',
    type: 'Boolean',
    get: (user) => {
      return user.customerData.tatus;
    },
    set: ((user, value) => {
      if (!user.customerData) {
        user.customerData = {};
      }
      user.customerData.tatus = value;
    })
  }],
  segments: [],
});

Hi @Lean_Junio! You seem to be defining smart fields here, is it intended ?

Hey Nicolas,

Yeah, I was hoping to have the ability to edit Level 1 and Level 2 fields.

Without the smart fields, they’re not editable. And as for the level 2 fields, they only appear as JSON editors.

Lean

I’m sorry but for now we don’t support this, you need to flatten the objects with your smart fields as you did. Otherwise you need to use the JSON Editor. I’ll push this feature request to our product team !

I see,

In that case, how do I flatten level 2 fields? Is there an example github repo by any chance?

Lean

You need to add specific smart fields for each nested property I guess.
Tell me if it does the trick !

But you shouldn’t need to add smart fields for the 1st nested level, it should be supported out of the box.

Hey Nicolas,

Thanks! The only trouble I’m having is that vehicles is actually an array. I’m trying to set/get specific vehicles. Will that still be the same code?

Thanks for the help!

Lean

Hi @Lean_Junio,

As stated by @anon94532230, that’s not supported out of the box for embedded data with lvl 2 nested fields.

Also, you will not be able to handle “complex” types with smart fields (In your case, array of objects). I would suggest to try using a smart collections to references your nested models, that might work for your use case (Documentation is available here).

Let me know if that does the trick :pray: