我收到错误" TypeError:无法读取属性'数量'未定义"在运行这种流星方法时。
前端电话:
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 to cart", "Success");
}
});
}
});
方法(在集合文件夹中):
Meteor.methods({
addToCart: function(quantity, listingId, cartId){
var listing = Listings.findOne(listingId);
if(quantity > listing.quantity)
throw new Meteor.Error(422, 'You cannot add that many to your 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 that many to your cart');
LineItems.update({ cartId: Session.get("cartId"), listingId: listingId}, { $inc: { quantity: quantity}});
toastr.success("Added to 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 }});
}
}
});
该方法的第5行导致错误,似乎列表未定义。当我订阅调用该方法的路由上的列表时,为什么会这样。我是否需要将其设为仅服务器方法?
答案 0 :(得分:0)
调用方法时
Meteor.call("addToCart", (quantity, listingId, cartId), function(err, res){
(
)
quantity, listingId, cartId
Meteor.call("addToCart", quantity, listingId, cartId, function(err, res){
当您编写f(a, (b, c, d), e)
时,(b, c, d)
实际上是comma operator,它会评估其所有操作数,然后返回最后一个操作数。所以这与f(a, d, e)
相同。