当我提交表单请求时,我收到错误[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
可能存在问题,但我不确定。知道如何解决这个问题吗?
答案 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');
// ...
Object
个auth
和master
属性,而不是function
或Model
本身:
// ...
exports.auth = mongoose.model('Auth', authSchema);
exports.master = mongoose.model('Master', masterPass);