two mongoose schemas are filter in one angular data table , but reference schemas is not filtering

two mongoose schemas are filter in one angular data table , but reference schemas is not filtering

samad Ullahsamad Ullah Posts: 3Questions: 1Answers: 0
edited June 25 in DataTables

two Schemas are mention below.


**birth_Certificate.js******

var schema = new Schema({
  baby_name: {type:String, default :''},
    baby_gender: {type:String, required:true},
  certificate_id: {type:String},
    baby_blood_group : {type:String},
    baby_weight : {type:String},
    baby_length : {type:String},
    time_of_birth : {type:String},
  patient_id : {type:mongoose.Schema.Types.ObjectId ,  ref: 'Patient'},
    life_status : {type:String} ,
  created_by : {
      type:mongoose.Schema.Types.ObjectId,
      ref : "User",
      required:true
    },
  updated_by : {
      type:mongoose.Schema.Types.ObjectId,
      ref : "User",
      required:false
    },
});
schema.plugin(timestamps,  {
  createdAt: 'created_at',
  updatedAt: 'updated_at'
});
schema.plugin(mongooseHistory);
schema.plugin(dataTables)
module.exports = mongoose.model('birth_certificate',schema);**

******patient.js******

var schema = new Schema({
  patient_id: { type: String },
  name: { type: String, required: true },
  relation: { type: String },
  age: { type: String },
  age_type: { type: String },
  blood_group: { type: String },
  cnic: { type: String },
  passport: { type: String },
  gender: { type: String, required: true },
  address: {
    city: { type: String },
    country: { type: String, required: true },
    location: { type: String },

  },
  contact: {
    mobile_no: { type: String, required: true },
    whatsapp_no: { type: String },
    email: { type: String },

  },
  invoice: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Invoice' }],
  status: { type: String, required: true },
  created_by: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User",
    required: true
  },

now i am sharing my birth-certificate.component.ts:

ngOnInit() {
    super.ngOnInit();
    this.dtOptions = {
      pagingType: 'full_numbers',
      pageLength: 10,
      serverSide: true,
      processing: true,
      ajax: (dataTablesParameters: any, callback) => {
        this.http
          .post<DataTablesResponse>(
            environment.serverURL+'/certificate/datatable',
            dataTablesParameters, {}
          ).subscribe(resp => {
            console.log("the response data in ngoninit method is " , resp);
            this.certificateListData = resp.data;

            callback({
              recordsTotal: resp.recordsTotal,
              recordsFiltered: resp.recordsFiltered,
              data: []
            });
          });
      },
      columns: [{data:'patient_id.patient_id'},{data:'patient_id.name'},{data:'baby_name'},{data:'baby_gender'},{data:'certificate_id'},{data:'life_status'},{data:'baby_weight'},{data:'baby_length'},{data:'baby_blood_group'},{data:'time_of_birth'}],
      
    };


  }

**now i am sharing my controller file " birth-certificateController.js" **

exports.index = function(req, res) {
  certificate_model.find({} , function(err , docs){
    console.log("The data is:" , docs);
    if(err){
      console.log("Error id :" , err);
      res.status(501).json({
        error: err
      });
    }
    var data = docs;
    res.status(200).json(docs);
  });

}

exports.dataTable = function(req,res){
  console.log(req.body.columns);
  certificate_model.dataTables({
    limit: req.body.length,
    skip: req.body.start,
    select : ('baby_name patient_id name baby_gender baby_blood_group life_status baby_weight baby_length time_of_birth relation certificate_id'),
    populate : ('patient_id'),
  //find:{ $or:[ {'status':'inactive'}, {'name':'farhan'} ]},
  //find:{'status':'active'},
  search: {
      value: req.body.search.value,
      fields: ['patient_id.patient_id' ,'patient_id.name','baby_name','baby_gender','baby_blood_group','life_status','baby_weight']
    },
    order: req.body.order,
    columns: req.body.columns
  }).then(function (table) {
    console.log("Data table response:" , res);
    res.json({
      data: table.data,
      recordsFiltered: table.total,
      recordsTotal: table.total
    });
  });

};

kindly suggest me how to filter my patient id in my data table , i am still working on it since last week but its not working ,,,
thanks in advance....

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • allanallan Posts: 54,714Questions: 1Answers: 8,560 Site admin

    serverSide: true,

    This means that the server-side script is responsible for doing all the sorting, filtering and paging. DataTables will submit data to the server tell the server what it needs to do.

    I don't know what the code behind certificate_model.dataTables is, so I can't say why it wouldn't be doing the filtering, but that is where I suggest you start the debugging.

    Allan

  • samad Ullahsamad Ullah Posts: 3Questions: 1Answers: 0

    Dear Allan!
    it is doing filtering but i want to filter reference Schema id knows as "patient_id", in my birth_certificate datatable here i am sending you a file , it will be clear



  • samad Ullahsamad Ullah Posts: 3Questions: 1Answers: 0

    kindly suggest any proper solution

  • allanallan Posts: 54,714Questions: 1Answers: 8,560 Site admin

    I'm afraid that writing server-side scripts for Mongoose is beyond the scope of the support that we can provide. If you can't sure how to query the database for the join type you want you might be best asking in a Mongoose specific forum.

    Allan

Sign In or Register to comment.