简体   繁体   English

Express.js MongoDB更新查询

[英]Expressjs mongodb update query

In condition books.update({"id": {$eq: bid}}, {$set:{"status": login}}... when i use variable bid it doesnt work but if i put smth like books.update({"id": {$eq: 1}}, {$set:{"status": login}} it works just fine. I have to use variable in my project. 在有条件的books.update({“ id”:{$ eq:bid}},{$ set:{“ status”:login}} ...中,当我使用可变出价时,它不起作用,但是如果我把东西像书一样。 update({“ id”:{$ eq:1}},{$ set:{“ status”:login}}的效果很好。我必须在项目中使用变量。

node code 节点代码

app.get("/logged/:login/borrow/:bid/confirm", function(req,res){
  var login = req.params.login;
  var bid = req.params.bid;
  MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Nawiązano połączenie z serwerem");
  var books = db.collection('books');
      books.update({"id": {$eq: bid}}, {$set:{"status": login}}, function(er, result){
      assert.equal(er, null);
      res.redirect('/logged/'+login+'/borrow');
      });
    db.close();
  });
});

Pasting hjs file in case 粘贴hjs文件以防万一

  <a href="/logged/{{nick}}/return/{{id}}/confirm"></a>

Maybe req.params.bid is giving you a string number hence it's not matching. 也许req.params.bid正在为您提供一个字符串号,因此它不匹配。 id field might be looking for 1 but you would be sending "1" in variable bid. id字段可能正在寻找1,但您将以可变出价发送“ 1”。

try to convert bid into a number and then check. 尝试将出价转换为数字,然后检查。

bid = parseInt(bid,10);

I'm guessing that id is a numeric value, since the query with a 1 works. 我猜测id是一个数字值,因为带有1的查询有效。

Request parameters are strings. 请求参数是字符串。 Convert the string to an integer and you should be fine. 将字符串转换为整数,就可以了。

var bid = parseInt(req.params.bid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM