获得TypeError

时间:2014-05-14 04:42:17

标签: javascript node.js express mongoose

当我提交表单请求时,我收到错误[TypeError: object is not a function]。 这是我的猫鼬代码:hiren-conf.js

var auth = require('../auth.js');
var mongoose = require( 'mongoose' );

mongoose.connect(auth['mongodb']);

var authSchema = new mongoose.Schema({
    tag: String,
    email: String,
    username: String,
    createdOn: Date,
    updatedOn: { type: Date, default: Date.now },
    url: String,
    password: String,
    icon: String
});

var masterPass = new mongoose.Schema({
    hash : String,
    tag : String,
    state: { type: String, default: false}
});

exports.auth = mongoose.model('Auth', authSchema);
exports.master = mongoose.model('Master', masterPass);

database.js代码:

var mongoose = require( 'mongoose' );
var auths = require('../model/hiren-conf');

exports.create = function(req){
    var instance = new auths();
    if(req.body.tag && req.body.email){
        auths.findOne({ 'tag' : req.body.tag , 'email' : req.body.email}, function(err , duplicate){
            if (!err){
                if(!duplicate){
                    instance.tag = req.body.tag;
                    instance.email = req.body.email;
                    instance.username= req.body.username;
                    instance.createdOn = Date.now();
                    instance.url = req.body.url;
                    instance.password = req.body.password;
                    instance.save(function(err){
                        if(!err) console.log('Saved');

                    });
                    return "Save";

            }       else return "Duplicate";
        } else console.log(err);
        });

        }

    };

exports.auth可能存在问题,但我不确定。知道如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

要引用'Auth'模型,您必须访问该属性auths.auth

exports.create = function(req){
    var instance = new auths.auth();
    if(req.body.tag && req.body.email){
        auths.auth.findOne(...);
// ...

由于auths是来自exports的{​​{1}}:

hiren-conf.js

它将是var mongoose = require( 'mongoose' ); var auths = require('../model/hiren-conf'); // ... Objectauthmaster属性,而不是functionModel本身:

// ...

exports.auth = mongoose.model('Auth', authSchema);
exports.master = mongoose.model('Master', masterPass);