Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js MongoDB检查现有值_Node.js_Mongodb_Authentication_Post_Npm - Fatal编程技术网

Node.js MongoDB检查现有值

Node.js MongoDB检查现有值,node.js,mongodb,authentication,post,npm,Node.js,Mongodb,Authentication,Post,Npm,在继续post路由之前,如何检查mongo/mongoose值是否存在 我的部分猫鼬模型如下所示: reserved: {type: Boolean, default: false} module.exports = mongoose.model("Rentals", rentalsSchema); 我一直在试着在没有任何运气的情况下用这种方式检查保留是否是真的 注意:正如您将在下面看到的,这需要为每个rentals ID工作,因此它需要以某种方式查找ByID router.put("/:

在继续post路由之前,如何检查mongo/mongoose值是否存在

我的部分猫鼬模型如下所示:

 reserved: {type: Boolean, default: false}
 module.exports = mongoose.model("Rentals", rentalsSchema);
我一直在试着在没有任何运气的情况下用这种方式检查保留是否是真的

注意:正如您将在下面看到的,这需要为每个rentals ID工作,因此它需要以某种方式查找ByID

router.put("/:id/reserve", middleware.checkRentalsStatus, function(req, res){


     //checking here for existing value
     //checking here for existing value

     if(Rentals.reserved == true){
            res.redirect("back");
            console.log('TAKEN!')
        } else {


 // Token is created using Stripe.js or Checkout!
// Get the payment token submitted by the form:
 var token = req.body.stripeToken; // Using Express

// Charge the user's card:
  var charge = stripe.charges.create({
  amount: 19500,
  currency: "usd",
  description: "",
  source: token,
}, function(err, charge) {
     if(err){
            req.flash("error", err.message);
            res.redirect("back");
        } else {


    var reservedby = req.body.reservedby;
    var reserved = true;
    var reservedemail = req.body.reservedemail;
    var newReservation = {reserved: reserved, reservedby: reservedby, reservedemail: reservedemail }
    Rentals.findByIdAndUpdate(req.params.id, {$set: newReservation}, function(err, rentals){
        if(err){
            req.flash("error", err.message);
            res.redirect("back");
        } else {

            req.flash("success","It's reserved for you. Thank you!");
            res.redirect("/rentals/" + rentals._id);
        }
    });

}
});

  console.log('charged')
});

在完成邮寄路线之前,我如何更改代码以检查保留是否为真?我正在检查后端的预订路线是否有租车服务

您可以直接检查这样的条件

  if(Rentals.reserved )
     { res.redirect("back"); console.log('TAKEN!') } 
        else { // continue with post route 
       }

这就是答案。最好的方法是创建一个中间件进行检查。其他答案的问题是他们找不到肾脏的ID。Rentals.findByID是实现此功能的关键:

//CHECK Rentals HAS NOT BEEN RENTED
middlewareObj.checkRentalsStatus = function(req, res, next){
    Rentals.findById(req.params.id, function(err, foundRentals){
    if (err){
        res.redirect("back");
    }

  if (foundRentals.reserved){
            req.flash("error", 'Oh no! It looks like someone else JUST rented this location. Your credit card was not charged.');
            res.redirect("back");
} else {
next();
}
});
}

请提供完整的代码。在这种情况下,什么是租金&你是如何从Mongo检索到价值的。就这样尝试,我通常会在我的代码中尝试。ifRentals.reserved{res.redirectback;console.log'take!'}else{//在输入此if条件之前,继续使用Rentals.reserved的post routeprint值。它对您有效吗。@AndrewLeonardi您的模式将reserved定义为布尔值。布尔值可以是真的,也可以是假的。不需要进行相等检查。