Home > other >  Missing data when using extend-schema in mongoose nodejs
Missing data when using extend-schema in mongoose nodejs

Time:11-10

missing data when using extend-schema in mongoose node js i need to create an user model and Client extends from user . i'm installing the "mongoose-extend-schema" package my model :

const userSchema = new mongoose.Schema({
      nom: String,
      prenom: String,
      email: String,
      password: String,
      confirm_password: String,
      matricule: String,
      fonction: String,
      role: {
        type: String,
        default: "anonyme",
      },
      activatedMail: { type: Boolean, default: false },
      activationToken: { type: String, default: "" },
      emailTokenExpires:{type:Date, default: null },
     
      resetPasswordToken: { type: String, default: "" },  
      resetPasswordExpires:{type:Date,default: null},
    Complement_adresse: String,
    ville: String,
    pays: String,
    cp: String,
    tel: String,
    rue: String,
    createdOn: { type: Date, default: Date.now },
    });
const User = mongoose.model("User", userSchema);

//Client extends from User model

    const clientSchema = extendSchema(userSchema, {
     
      rep:{
        type: String,
        default:"EA000"
      },
    
       Src_Client: {
        type: String,
        enum: ["prospect","externe"],
        default:"externe"
      },
      Date_src: {
        type: String,
      },
      Date_StatQ: {
        type: String,
      },
      Date_StatCC: {
        type: String,
      },
      Date_StatCp: {
        type: String,
      },
      Date_StatCs: {
        type: String,
      },
      Date_StatEs: {
        type: String,
      },
      descriptionClient: {
        type: String,
      }
    });
exports.userSchema = userSchema;
exports.User = User;

when i create a new Client extends from user mode some data are missing,the response take only the data from user and ignore data from client

client = new User({
      matricule: matClient,
     
      nom: req.body.nom,
      role:"Client",
      prenom: req.body.prenom,
      fonction: req.body.fonction,
      descriptionClient:req.body.descriptionClient,
      email: req.body.email,
      tel: req.body.tel,
      rue: req.body.rue,
      ville: req.body.ville,
      pays: req.body.pays,
      type: req.body.type,
      email2: req.body.email2,
      Complement_adresse: req.body.Complement_adresse,
      cp: req.body.cp,
      tel2: req.body.tel2,
      Src_Client: "prospect",
      Date_src: req.body.Date_src,
      Date_StatQ: req.body.Date_StatQ,
      Date_StatCC: req.body.Date_StatCC,
      Date_StatCp: req.body.Date_StatCp,
      Date_StatCs: req.body.Date_StatCs,
      Date_StatEs: req.body.Date_StatEs
    });

this is an example with postman

here

CodePudding user response:

just add the same properties in client model in the user model:

    const userSchema = new mongoose.Schema({
      nom: String,
      prenom: String,
      email: String,
      password: String,
     //other fields...

    
Add properties of client here
    rep:{
      type: String,
      default:"EA000"
    },
    
     Src_Client: {
      type: String,
      enum: ["prospect","externe"],
      default:"externe"
    },
    Date_src: {
      type: String,
    },
    Date_StatQ: {
      type: String,
    },
    Date_StatCC: {
      type: String,
    },
    Date_StatCp: {
      type: String,
    },
    Date_StatCs: {
      type: String,
    },
    Date_StatEs: {
      type: String,
    },
    descriptionClient: {
      type: String,
    },
    
    });

you can see this link for more details : https://www.npmjs.com/package/mongoose-extend-schema

  • Related