Mongoose静态外部文件

时间:2016-02-11 20:55:30

标签: javascript node.js mongoose

我试图在外部文件中声明一个猫鼬模型的静态函数:

user.js的

var userSchema = Schema({
    username: {type: String},
    password: {type: String},
});

userSchema.statics = userLogic.statics;

User = mongoose.model('User', userSchema);
module.exports.userModel = User;

logic.js

...
var User = require('../models/user').userModel;

module.exports.statics = {
    create: function (username, password, callback) {
        var user = new User({username: username, password: password});
        user.save(function (err) {
            if (err) throw err; else callback(user);
        });
    }

当我致电userModel.create(user, pass, callback)时,我在创建功能行中遇到异常:var user = new User({username: username, password: password})说:

TypeError: User is not a function
    at Function.module.exports.statics.create
    ...

我该如何解决这个问题?关于如何将静态保存在外部文件中的任何想法?

0 个答案:

没有答案