2017-06-30 121 views
1

我无法弄清楚这一点。用完燃料。需要指导。通过html输入

我只是希望我的购物车更新到html页面中表单中放置的项目数量。起初这似乎是一件容易的事,但几小时后几小时,我无法弄清楚。我在想身体解析器,但不知道如何实现它。

我试图抓住产品ID和身体解析它在我的后路线一次,但我似乎无法得到正确的替换数量。我还试图查看是否有方法将身体解析器表单数据发送到我的更新函数......但是没有运气。

我知道有人会说“哦,这很容易,你不能得到这个???”但我真的不知道该去哪里去帮助我完成这个任务。

这里是我的终端

Bag { 
    items: { '5952b830d9b287b339b92dc3': { item: [Object], qty: 'How do i link this to req', price: 350 } }, 
    totalQty: 1, 
    totalPrice: 350, 
    add: [Function], 
    update: [Function], 
    reduceByOne: [Function], 
    removeItem: [Function], 
    generateArray: [Function] } 
POST /updatebag/5952b830d9b287b339b92dc3 302 285.498 ms - 96 
GET /bag/ 304 179.207 ms - - 

这是我更新的购物车方法

this.update = function(req, id) { 
     var storedItem = this.items[id]; 


     storedItem.qty = 'How do i link this to req'; 

    }; 

这是帖子的路线

router.post('/updatebag/:id', function(req, res, next) { 
    var productId = req.params.id; 
    var bag = new Bag(req.session.bag ? req.session.bag : {items: {}}); 

    Product.findById(productId, function(err, product) { 
     if (err) { 
      return res.redirect('/'); 
     } 
     bag.update(product, product.id) 
     req.session.bag = bag; 
     console.log(req.session.bag); 
     res.redirect('back'); 
    }); 
}); 

编辑:对不起我筋疲力尽哈哈完全忘了提及我试图改变产品的数量,可以通过文本“我如何链接到需求”来查看在我的终端中。

回答

0

我解决了这个问题!这有点混乱,但如果你有问题,你可以尝试做这样的事情。如果你有任何批评,请不要犹豫,打你所有的东西。

我的新航站楼

Bag { 
    items: { '5952b830d9b287b339b92dc3': { item: [Object], price: 350, qty: '29' } }, 
    totalQty: 1, 
    totalPrice: 350, 
    add: [Function], 
    update: [Function], 
    reduceByOne: [Function], 
    removeItem: [Function], 
    generateArray: [Function] } 
POST /updatebag/5952b830d9b287b339b92dc3 302 163.569 ms - 96 
GET /bag/ 200 176.010 ms - 7497 

新更新的购物车方法

this.update = function(req, id) { 
     var storedItem = this.items[id]; 
     storedItem.qty = updateqty.qty; 

    }; 

新话题路线

router.post('/updatebag/:id', function(req, res, next) { 
    var productId = req.params.id; 
    var bag = new Bag(req.session.bag ? req.session.bag : {items: {}}); 

    Product.findById(productId, function(err, product) { 
     if (err) { 
      return res.redirect('/'); 
     } 
     updateqty.qty = req.body.qty; 
     bag.update(product, product.id); 

     req.session.bag = bag; 
     console.log(req.session.bag); 
     res.redirect('back'); 
    }); 
}); 

的新架构添加我为我的更新数量

var mongoose = require('mongoose'); 
var mongoosastic = require("mongoosastic"); 
var Schema = mongoose.Schema; 

var updateqty = new Schema({ 
    qty: { type: String, unique: true, lowercase: true}, 
}); 


module.exports = mongoose.model('updateqty', updateqty);