我正在努力为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'
谁能看到这个问题?
最佳答案
您如何定义架构?通常,您会执行以下操作:
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();
关于node.js - 用 Mongoose 导出模型函数时出现问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6886388/