我正在努力为Mongoose模型创建模型函数。我在这里定义了一个方法:
Schema.listingSchema.method('applyPrice', function() {
this.price = priceFromString(this.title);
});
我在这里访问它:
var listing = new Listing();
// assign all relevant data
listing.title = title;
...
// pull the price out of the title and description
listing.applyPrice(listing);
,其中
Listing = mongoose.model('Listing', Schema.listingSchema);
我收到错误:
TypeError: Object #<model> has no method 'applyPrice'
有人能看到这个问题吗?
答案 0 :(得分:0)
您如何定义架构?通常你会做这样的事情:
var listingSchema = new mongoose.Schema({
title: String
});
listingSchema.method('applyPrice', function() {
this.price = priceFromString(this.title);
});
mongoose.model('Listing', listingSchema);
var Listing = mongoose.model('Listing');
var listing = new Listing({ title: 'Title' });
listing.applyPrice();