Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Lana Toogood
10,158 PointsOOJS problem with class method
Hi, I'm still learning OOJS and wondering if someone can help. Every time I call this method (Item.parseDocToItem(doc)) I get "Uncaught (in promise) TypeError: Item.parseDocToItem is not a function". Is there a problem with the way I've declared this method or is it something else? Thanks
// ITEM OBJECT //
class Item {
constructor(name, description, itemNo, size, price, imgURL, id){
this.name = name;
this.description = description;
this.itemNo = itemNo;
this.size = size;
this.price = price;
this.imageURL = imgURL;
this.id = id;
this.sold = false;
}
/**
* Returns new item with item doc data from Firestore.
* @return {Object} item - new inventory item.
*/
parseDocToItem(doc) {
const name = doc.data().name;
const description = doc.data().description;
const itemNo = doc.data().itemNo;
const size = doc.data().size;
const price = doc.data().price;
const imageUrl = doc.data().imageURL;
const id = doc.id;
let item = new Item(name, description, itemNo, size, price, imageUrl, id);
if (typeof(item.name) != 'string' || item.name == null) {
console.log('item name is not valid');
return null;
} else if (typeof(item.description) != 'string' || item.description == null) {
console.log('item description is not valid');
return null;
} else if (typeof(item.itemNo) != 'string' || item.itemNo == null) {
console.log('item no is not valid');
return null;
} else if (typeof(item.size) != 'string' || item.size == null) {
console.log('item size is not valid');
return null;
} else if (typeof(item.price) != 'number' || item.price == null) {
console.log('item price is not valid');
return null;
} else if (typeof(item.imageUrl) != 'string' || item.imageUrl == null) {
console.log('item url is not valid')
return null;
} else if (typeof(item.id) != 'string' || item.id == null) {
console.log('item id is not valid');
return null;
} else {
return item;
}
}
}
1 Answer
scribbles
21,173 PointsMake sure you are instantiating a new instance of Item.
let thing = new Item('Thing', 'Brand new thing', 12, 'M', 12.99, 'https://placeimg.com/320/160/animals', 111)
// Then you can call the method on the item instance:
thing.parseDocToItem(doc);
Lana Toogood
10,158 PointsLana Toogood
10,158 Pointsthank you, a new instance of item is instantiated in the above code but it's tricky to see, because I haven't spaced the code very well. I think I may have solved the problem though - I needed to add the "static" keyword when declaring the parseDocToItem(doc) method inside the class. Now it seems to be working!