Skip to content

Cannot display the Incremented id for a nested field #108

Description

@abedahmeduhp

The problem exists with the field idIncPlan in the second document, it doesn't display properly like other fields

output :

[
    {
        "_id": "6098a205a0d57178fed75442",
        "libelleMateriel": "mat 1",
        "weeks": [
            {
                "state": "0",
                "_id": "6098a205a0d57178fed75443",
                "week": 1,
                "idIncPlan": 86
            },
            {
                "state": "0",
                "_id": "6098a205a0d57178fed75444",
                "week": 2,
                "idIncPlan": 85
            }
        ],
        "__v": 0,
        "createdAt": "2021-05-10T03:01:25.029Z",
        "updatedAt": "2021-05-10T03:01:25.053Z",
        "idIncmateriels": 55
    },
    {
        "_id": "6098a20da0d57178fed75445",
        "libelleMateriel": "mat 1",
        "weeks": [
            {
                "state": "0",
                "_id": "6098a20da0d57178fed75446",
                "week": 1
            },
            {
                "state": "0",
                "_id": "6098a20da0d57178fed75447",
                "week": 2,
                "idIncPlan": 87
            }
        ],
        "__v": 0,
        "createdAt": "2021-05-10T03:01:33.034Z",
        "updatedAt": "2021-05-10T03:01:33.044Z",
        "idIncmateriels": 56
    },
    {
        "_id": "6098a212a0d57178fed75448",
        "libelleMateriel": "mat 1",
        "weeks": [
            {
                "state": "0",
                "_id": "6098a212a0d57178fed75449",
                "week": 1,
                "idIncPlan": 90
            },
            {
                "state": "0",
                "_id": "6098a212a0d57178fed7544a",
                "week": 2,
                "idIncPlan": 89
            }
        ],
        "__v": 0,
        "createdAt": "2021-05-10T03:01:38.278Z",
        "updatedAt": "2021-05-10T03:01:38.290Z",
        "idIncmateriels": 57
    },
    {
        "_id": "6098a219a0d57178fed7544b",
        "libelleMateriel": "mat 1",
        "weeks": [
            {
                "state": "0",
                "_id": "6098a219a0d57178fed7544c",
                "week": 1
            },
            {
                "state": "0",
                "_id": "6098a219a0d57178fed7544d",
                "week": 2
            }
        ],
        "__v": 0,
        "createdAt": "2021-05-10T03:01:45.910Z",
        "updatedAt": "2021-05-10T03:01:45.919Z",
        "idIncmateriels": 58
    }
]

Schema :

const mongoose = require("mongoose");
const AutoIncrement = require('mongoose-sequence')(mongoose);

const subSchema = new mongoose.Schema({
  week: {
    type: Number,
  },
  idIncPlan: {
    type: Number,
  },
  state: {
    type: String,
    default: "imprimé",
  },
});


const materielModel = mongoose.Schema(
  {
    weeks: {
      type: [subSchema],
    },

    libelleMateriel: {
      type: String,
      required: "{libelleMateriel} is required!",
    },
    
    idIncmateriels: {
      type: Number,
    },

  },
  {
    timestamps: true,
  }
);
subSchema.plugin(AutoIncrement, {inc_field: 'idIncPlan',disable_hooks: true});

materielModel.plugin(AutoIncrement, {inc_field: 'idIncmateriels'});

module.exports = mongoose.model("materiel", materielModel);

Controller :

    const materiels = await Materiel.insertMany(dataJson);

    materiels[0].setNext("idIncmateriels", function (err, user) {
      
      if (err) console.log("Cannot increment the rank because ", err);
    });
    

materiels[0].weeks[0].setNext("idIncPlan", function (err, user) {
      if (err) console.log("Cannot increment the rank because ", err);
    });



materiels[0].weeks[1].setNext("idIncPlan", function (err, user) {
      if (err) console.log("Cannot increment the rank because ", err);
    }

    res.send(materiels);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions