这个问题在这里已有答案:
我想确保这两行(antecedants:[req.query.antecedants] +info.antecedants.push([req.query.antecedants])
,因为它从代码中显示的前提是一个字符串数组所以在我的查询中我想添加一个字符串数组但是当我调用这个方法(infopatient
)时它会添加所有的值,除了阵列(空)。
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);
});
})
线条antecedants:[req.query.antecedants]
和info.antecedants.push([req.query.antecedants])
对我来说似乎是多余的。你应该只需要第一个就可以了。如果要更新现有对象,则只需要使用push
。
如果你没有看到你的antecedants
阵列被填充,我建议你确保你的req.query.antecedants
被定义。
但antecedants:[req.query.antecedants]
似乎也很脆弱。命名表明可以在此处将多个字符串值传递给antecedants
。它以逗号分隔吗?如果是这样,你可能想要这样的东西:
antecedants: req.query.antecedants ? req.query.antecedants.split(',') : []
如果它不是逗号分隔,你可以简单地这样做:
antecedants: req.query.antecedants ? [req.query.antecedants] : []