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 trialSean Urgel
Courses Plus Student 6,650 PointsHi guys - working on a personal project need some help in my REST API :)
const itemSchema = new Schema({
type:String,
brand:String,
description:String,
model:String,
price:Number,
createdAt:{type: Date, default: Date.now},
updatedAt:{type: Date, default: Date.now}
});
itemSchema.statics.findBrand = function(brand, callback){
return this.find({brand: brand}, callback);
}
itemSchema.statics.findType = function(type, callback){
return this.find({type: type}, callback);
}
itemSchema.methods.findSameBrand = function(callback){
// this == docuemnt
return this.model("Item").find({type: this.type}, callback);
}
itemSchema.method("update", function(updates, callback){
Object.assign(this, updates, { updatedAt: new Date() });
this.parent().save(callback);
});
const Item = mongoose.model("Item", itemSchema);
const mouse = new Item({
type: "Mouse",
brand: "Logitech",
description: "Simple, yet elegant.",
model: "G102",
price: 3000
});
const itemData = [
{
type: "Mouse",
brand: "Corsair",
description: "2",
model: "G104",
price: 8000
},
{
type: "Mouse",
brand: "Razer",
description: "3",
model: "G103",
price: 2000
},
{
type: "Keyboard",
brand: "Razer",
description: "3",
model: "G103",
price: 2000
},
mouse
]
Item.remove({}, function(err) {
if (err) console.error(err);
Item.create(itemData, function(err, items) {
if(err) console.error(err);
Item.findOne({type: "Mouse"}, function(err, mouse){
mouse.findSameBrand( (err, items) => {
if (err) console.error(err);
items.forEach( items => {
console.log(`Type: ${items.type} - Brand: ${items.brand} Price: ${items.brand} `);
});
});
db.close( () => {
console.log("connection closed");
});
});
});
});
based on the following code: I am getting this error
events.js:182
throw er; // Unhandled 'error' event
^
TypeError: this.model is not a function
Can someone help? This doesn't look wrong at all to me