A mistake in "Building an E-commerce Application with MEAN"
Closed this issue · 1 comments
soloviola commented
Hey Adrian,
My name is Guangyao Miao from New York and I am learning MEAN stack for developing my web app. I really enjoy reading your book "Building an E-commerce Application with MEAN"!
I am finishing reading chapter 2: "Building an amazing store frontend with angular JS" and I just found a mistake in the code: in Cruding product with Angular JS from Chapter 2. The code:
/* clients/app/products/products.service.js */
angular.module('meanshopApp')
.factory('Product', function () {
var last_id = 5;
var example_products = [
{_id: 1, title: 'Product 1', price: 123.45, quantity: 10, description: 'Lorem ipsum dolor sit amet'},
{_id: 2, title: 'Product 2', price: 123.45, quantity: 10, description: 'Lorem ipsum dolor sit amet'},
{_id: 3, title: 'Product 3', price: 123.45, quantity: 10, description: 'Lorem ipsum dolor sit amet'},
{_id: 4, title: 'Product 4', price: 123.45, quantity: 10, description: 'Lorem ipsum dolor sit amet'},
{_id: 5, title: 'Product 5', price: 123.45, quantity: 10, description: 'Lorem ipsum dolor sit amet'}
];
return {
query: function(){
return example_products;
},
get: function(product){
var result = {};
angular.forEach(example_products, function (product) {
if(product._id == params.id)
return this.product = product;
}, result);
return result.product;
},
delete: function(params){
angular.forEach(example_products, function (product, index) {
if(product._id == params._id){
console.log(product, index);
example_products.splice(index, 1);
return;
}
});
},
create: function(product){
product.id = ++last_id;
example_products.push(product);
},
update: function(product){
var item = this.get(product);
if(!item) return false;
item.title = product.title;
item.price = product.price;
item.quantity = product.quantity;
item.description = product.description;
return true
}
};
});
in the get function the input should be:
get: function(param){
Or it cannot get context of param.
FYI, I am reading on Safari online book.
Cheers,
Guangyao
amejiarosario commented
@soloviola Yes, you are totally right. This is already fixed on the official repo: https://github.com/amejiarosario/meanshop/blob/ch2/client/app/products/products.service.js