我遇到的问题是,猫鼬不让我实例化一个不同于'pre'方法的模式类型的对象架构。Mongoose TypeError:实例化模式类型的对象时,对象不是函数
我有2个模式 - '用户'和'Tickit'。
user.js的
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var Schema = mongoose.Schema;
var Tickit = require('../models/Tickit');
var userSchema = new Schema({
email : String,
password : String,
tickits : [Tickit.tickitSchema]
});
userSchema.methods.generateHash = function(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
userSchema.methods.validPassword = function(password) {
return bcrypt.compareSync(password, this.password);
};
module.exports = mongoose.model('User', userSchema);
和Tickit.js
var mongoose = require('mongoose');
var User = require('../models/User');
var Schema = mongoose.Schema;
var tickitSchema = new Schema({
title: String,
description: String,
author : { type: Schema.Types.ObjectId, ref: 'User' },
comments: [{body:"string", by: mongoose.Schema.Types.ObjectId}]
});
tickitSchema.pre('save', function(next){
var user = new User();
user.tickits.push ({id:this._id});
user.save(function(err){
if(err)
res.send(err)
.populate('tickits')
.exec(function(err, blah){
if(err) res.send(err);
})
res.json(blah);
})
next();
})
module.exports = mongoose.model('Tickit', tickitSchema);
我尝试在TickIT审核预方法做的就是填充 'Tickits' 阵列在用户模式与每次创建Tickit时该Tickit的ID。
然而,在我的应用程序,当我做创建TickIT审核,该应用程序崩溃,我得到这个错误
var user = new User();
^
TypeError: object is not a function
你找到答案吗? – Erez