Woodshop
Search…
Display/edit a nested document
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.

Requirements

    An admin backend running on forest-express-mongoose

How it works

Directory: /models

This directory contains the users.js file where the model is declared.
/models/users.js
1
const mongoose = require('mongoose');
2
3
const schema = mongoose.Schema({
4
'person': {
5
firstname: String,
6
lastname: String,
7
email: String,
8
},
9
}, {
10
timestamps: false,
11
});
12
13
module.exports = mongoose.model('users', schema, 'users');
Copied!

Directory: /forest

This directory contains the users.js file where the Smart Field fistnameis declared.
/forest/users.js
1
const { collection } = require('forest-express-mongoose');
2
3
collection('users', {
4
fields: [{
5
field: 'firstname',
6
type: 'String',
7
// Display the firstname
8
get: (user) => {
9
return user.person.firstname;
10
},
11
// Edit the firstname
12
set: ((user, value) => {
13
if (!user.person) {
14
user.person = {};
15
}
16
user.person.firstname = value;
17
})
18
}],
19
});
Copied!
Last modified 1yr ago