Wednesday, 15 April 2015

javascript - Meteor method cannot read property "quantity" undefined -



javascript - Meteor method cannot read property "quantity" undefined -

i'm getting error "typeerror: cannot read property 'quantity' of undefined" when running meteor method.

frontend call:

template.listingpage.events({ "submit #add-to-cart": function(e,t){ e.preventdefault(); var quantity = parseint($("#spinner-01").val()); var listingid = t.data._id; if(!session.get("cartid")){ var id = carts.insert({ line_items: []}); session.set("cartid", id); } var cartid = session.get("cartid"); meteor.call("addtocart", (quantity, listingid, cartid), function(err, res){ if(err){ toastr.error(err.reason, "error"); } else{ toastr.success("added cart", "success"); } }); } });

method ( in collection folder ):

meteor.methods({ addtocart: function(quantity, listingid, cartid){ var listing = listings.findone(listingid); if(quantity > listing.quantity) throw new meteor.error(422, 'you cannot add together many cart'); var lineitem = lineitems.find({ cartid: cartid, listingid: listingid }); if(lineitem){ //in cart var cartquantity = lineitem.quantity; if ( (cartquantity + quantity) > listing.quantity) throw new meteor.error(422, 'you cannot add together many cart'); lineitems.update({ cartid: session.get("cartid"), listingid: listingid}, { $inc: { quantity: quantity}}); toastr.success("added cart", "success"); } else{ // not in cart lineitem = { cartid: cartid, listingid: listingid, quantity: quantity, imageid: listing.imageobj._id } lineitems.insert(lineitem); carts.update(cartid, { $inc: { count: 1 }}); } } });

line 5 on method causing error, seems listing undefined. why case when subscribed listing on route calls method. need create server method?

when phone call method

meteor.call("addtocart", (quantity, listingid, cartid), function(err, res){

you shouldn't have ( ) around quantity, listingid, cartid:

meteor.call("addtocart", quantity, listingid, cartid, function(err, res){

when write f(a, (b, c, d), e), (b, c, d) comma operator, evaluates operands, returns lastly one. same f(a, d, e).

javascript jquery mongodb meteor

No comments:

Post a Comment