【问题标题】:add an array of string in query nodejs [duplicate]在查询nodejs中添加一个字符串数组[重复]
【发布时间】:2019-07-28 10:42:18
【问题描述】:

我想确定这两行(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);
    });          
  })

【问题讨论】:

  • 当您执行info.antecedants.push([req.query.antecedants]) 时,我不认为这是您架构中的有效类型,因为info.antecedants 已经是一个数组,并且您正在将一个嵌套数组添加到数据模型中。
  • 谢谢,但我知道问题出在那一行(推送),我不知道如何解决。
  • 只是想知道,当您设置声明new fiches 时,为什么要推动它?或者你试过info.antecedants.push(req.query.antecedants) 吗?
  • 实际上这个方法 get("/infopatient") 在我的 android 项目中被调用,当我删除了info.antecedants.push(req.query.antecedants) 这个字段在数据库中显示为空,当我添加它时,出现列前项但值为空。 ps:其他属性添加成功

标签: javascript node.js mongodb


【解决方案1】:

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] : []

【讨论】:

    猜你喜欢
    • 2020-12-19
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    相关资源
    最近更新 更多