0

I want to make sure of these two lines (antecedants:[req.query.antecedants] +info.antecedants.push([req.query.antecedants]) as it shown from the code antecedents is an array of string so in my query I want to add an array of string but when I called this method(infopatient) it adds all the values except the array(empty).

var mongoose = require('mongoose');
//define schema
var Schema= mongoose.Schema;
var ficheSchema= Schema({
  Datevisite: Date,
  Age:Number ,
  Pouls : Number ,
  TA : Number ,
  temperature : Number ,
  FR : Number,
  SAO2 : Number ,
  CGS : Number,
  antecedants : [String] ,
  motif :  String ,
  EVA:Number,
  id_patient : String
})



var fiche = mongoose.model('fiche',ficheSchema);
module.exports= fiche ;
//
app.get("/infopatient", (req, res) =>{
    var info = new fiches ( {
        Age:req.query.Age,
         Pouls : req.query.Pouls,
        TA:req.query.TA,
        temperature:req.query.temperature,
      FR:req.query.FR,
      SAO2:req.query.SAO2,
      CGS:req.query.CGS,
      EVA:req.query.EVA ,
      antecedants:[req.query.antecedants],
      Tmotif:req.query.motif}
      );
     info.antecedants.push([req.query.antecedants])
     info.save(function(err)
     {
        if(err) return handleError(err);
    });          
  })
Irf
  • 4,285
  • 3
  • 36
  • 49
  • 1
    When you do `info.antecedants.push([req.query.antecedants])`, I don't believe this is a valid type in your schema because `info.antecedants` is already an array, and you're adding a nested array into the data model. – mralanlee Mar 06 '19 at 17:19
  • Thank you but i know that the problem is in that line (push) and i don't know how to resolve it. – Chaima Besbes Mar 06 '19 at 17:31
  • Just wondering, why are you pushing to it when you set upon declaring `new fiches`? Alternatively have you tried `info.antecedants.push(req.query.antecedants)` ? – mralanlee Mar 06 '19 at 17:37
  • Actualy this method get("/infopatient") is called in my android project , and the application shows a form which the user should put the values to create a new fiche , when i deleted the line of info.antecedants.push(req.query.antecedants) this field is showed empty in the database and when i add it ,the column antecendents appear but the value is empty . Ps : the other attributes are successfully added – Chaima Besbes Mar 06 '19 at 18:11

1 Answers1

0

The lines antecedants:[req.query.antecedants] and info.antecedants.push([req.query.antecedants]) seem redundant to me. You should only need the first one for this to work. You would only need to use push if you were updating an existing object.

If you're not seeing your antecedants array being populated I would suggest you make sure your req.query.antecedants is defined.

But also antecedants:[req.query.antecedants] seems brittle. The naming suggests that multiple string values can be passed in to antecedants here. Is it comma separated? If so, you might want something like this for example:

antecedants: req.query.antecedants ? req.query.antecedants.split(',') : []

If its not comma separated, you could instead simply do this:

antecedants: req.query.antecedants ? [req.query.antecedants] : []
Andrew Lavers
  • 8,023
  • 1
  • 33
  • 50