This example shows you how to use a smart field to display and update a nested document.
Nested document in Forest Admin are not visible in the table view and are are displayed as a JSON in the details view. To make the document information more visible you can choose to display a smart field.
An admin backend running on forest-express-mongoose
This directory contains the users.js
file where the model is declared.
/models/users.jsconst mongoose = require('mongoose');​const schema = mongoose.Schema({'person': {firstname: String,lastname: String,email: String,},}, {timestamps: false,});​module.exports = mongoose.model('users', schema, 'users');
This directory contains the users.js
file where the Smart Field fistname
is declared.
/forest/users.jsconst { collection } = require('forest-express-mongoose');​collection('users', {fields: [{field: 'firstname',type: 'String',// Display the firstnameget: (user) => {return user.person.firstname;},// Edit the firstnameset: ((user, value) => {if (!user.person) {user.person = {};}user.person.firstname = value;})}],});