我有一個名爲Cart
的客戶端端本地集合,其中包含產品對象。我想要做的是將模板中所有產品和數量的總和返回到模板中。我這樣做如下:流星Mongo.Cursor#地圖沒有預期的效果
Template.layout.helpers({
cartTotal:() => {
if(!Cart.findOne({})) {
return 0;
}
else {
let productsCursor = Cart.find({});
let totalPrice = 0;
productsCursor.map((product) => {
let subtotal = product.qty*product.price;
totalPrice =+ subtotal;
console.log(totalPrice);
});
return totalPrice.toFixed(2);
}
}
});
一切都很好地總結時,我將同樣的產品以集合(該product.qty
增加1),但是當我添加另一個目的是在Cart
集合,它開始總結只有這個物體的數量和價格。
如果我在瀏覽器控制檯上檢查集合,所有對象都在那裏,但cartTotal
方法的結果未返回正確的值。
我已經試過使用Mongo.Cursor#forEach()
方法,但結果是一樣的。我怎樣才能達到我想要做的?哪裏不對???
遺憾的是它不工作... :(不過感謝您的回答。 – kemelzaidan