We want to create a category at the front end with a parent category and a child category below. In the database these data are all horizontal, we add the association to the data to form a chain structure.

Write and create category pages

Write a request parent classification method

Modify the server model

In order to save the parent classification, we need to modify the classification model:

const mongoose = require('mongoose')

// Each schema is a document, and each model is a collection.
const schema = new mongoose.Schema({
    cateName: String.parent: { type: mongoose.Schema.Types.ObjectId, ref: 'Category'}})module.exports = mongoose.model('Category', schema)
Copy the code

Add a column that shows the parent classification

But we want to see the name of the parent class instead of the ID, so change the server interface:

Modify the server interface

At this point, parent becomes an object

View browser:

Work completed…..