TypeError:Object#<object>在exports.save </object>中没有方法'findById'

时间:2014-06-06 21:54:25

标签: javascript node.js mongodb express

我在这里找到了其中的几个问题,但是我发现这些问题主要涉及HTML,我在提交java登录时遇到了这个问题。

非常简单的保存功能

var model = require('../../models/User')
exports.save= function(req,res){
  model.findById(req.body.id,function(err,doc){
    if(!doc) doc = new model()
    doc.email = req.body.email
    doc.password = req.body.password
    doc.save(function(err){
      if(err){
        req.flash('error',err)
      }
      else{
        req.flash('success','User saved')
        res.redirect('/')
      }
    })
  })
}

我的模型很长但是这里有一个样本

//define schema
schema = new mongoose.Schema({
  email: {
    label: 'Email',
    type: String,
    lowercase: true,
    unique: true,
    required: true,
    index: true,
    validate: [
      validate('len','6','100'),
      validate('isEmail')
    ]
  },
  password: {
    label: 'Password',
    type: String,
    required: true,
    select: false,
    get: function(){ return '********' },
    set: function(v){
      return bcrypt.hashSync(v,bcrypt.genSaltSync(12))
    },
    validate: [
      validate('len','8','64')
    ]
  },
model = mongoose.model('User',schema)
exports.name = 'user'
exports.description = 'User Model'
exports.schema = schema
exports.model = model

通过模态提交到该功能,并在每次提交时提交错误

TypeError: Object #<Object> has no method 'findById' at exports.save

非常感谢任何帮助或想法

1 个答案:

答案 0 :(得分:0)

更改

var model = require('../../models/User')

var model = require('../../models/User').model

另见the documentation on modules