【问题标题】:Mongoose query find() returns nothingMongoose 查询 find() 什么也不返回
【发布时间】:2018-05-22 05:59:37
【问题描述】:

我正在使用 2 个条件(关键字和用户 ID)查询 db,它们都在控制台日志中正确返回,但不返回任何内容,不返回 null,或者 [],只是空白。语法有问题?

app.get("/api/client?", function (req, res) {

console.log("Search > " + JSON.stringify(req.query));
//Search > {"keyword":"emily","filterby":"5a25f3d1d4b3e30792dd53ca"}

var keyword = req.query.keyword;
var user_id = req.query.id;

Client.find({
  $and: [{
      "firstname": new RegExp('^' + keyword + '$', "i")
    },
    {
      userid: user_id
    }
  ]
}, (err, result) => {
  if (err) {
    console.log(err);
  }
  res.status(200).json(result);
  console.log("Result is... " + result)
  //Result is

});
});

或者,我尝试不使用查询,只需根据用户 ID 搜索所有内容即可,它会返回 db 中的所有记录

app.get("/client", function (req, res) {
var user_id = req.query.id;

Client.find({
  userid: user_id
}, (err, result) => {
  if (err) {
    console.log(err);
  }
  res.status(200).json(result);

});
});

【问题讨论】:

    标签: angularjs mongoose


    【解决方案1】:

    找到了解决办法!菜鸟错误,我赶上了搜索所有工作的内容,var user_id = req.query.id 但实际上当我传入“filterby”时,基于

    console.log {"keyword":"emily","filterby":"5a25f3d1d4b3e30792dd53ca"} 
    

    应该是这样的

    var keyword = req.query.keyword;
    var **filterby** = req.query.id;
    
    Client.find({
       $and: [{
           "firstname": new RegExp('^' + keyword + '$', "i")
           },
           {
           userid: **filterby**
           }
         ]
       }
    

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 2020-11-03
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 2014-12-04
      • 2022-08-18
      • 2019-03-19
      • 2013-09-25
      相关资源
      最近更新 更多