【问题标题】:Gettings TypeError获取类型错误
【发布时间】:2014-05-14 13:11:03
【问题描述】:

当我提交表单请求时,我收到错误 [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 可能有问题,但我不确定。知道如何解决这个问题吗?

【问题讨论】:

  • 我猜错误是从var instance = new auths(); 内部exports.create 抛出的。如果auths 是来自hiren-conf.jsexports,那么它将不是function。这将是一个带有authmaster 属性的Object。也许试试var instance = new auths.auth();。否则,错误发生在哪一行?堆栈跟踪应包括文件名和行号。
  • 也试过了。新错误 TypeError: Object # has no method 'findOne'
  • 感谢它现在工作:)

标签: javascript node.js express mongoose


【解决方案1】:

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

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

因为auths 是来自hiren-conf.jsexports

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

// ...

这将是一个普通的Object,带有authmaster 属性,而不是functionModel 本身:

// ...

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

【讨论】:

    猜你喜欢
    • 2020-11-19
    • 2011-08-12
    • 2017-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 2019-05-08
    • 2012-01-26
    相关资源
    最近更新 更多