2016-05-30 67 views
7

我不断收到一个问题,即newUser.save()不是函数。这是我以前用过的猫鼬功能。我正确地要求猫鼬,并且不确定为什么发生这个错误。欢迎任何帮助。.save()不是函数Mongoose

我得到的错误是TypeError: newUser.save is not a function

我user.js的模型文件夹内

var mongoose = require('mongoose'); 
var bcrypt = require('bcryptjs'); 
var Schema = mongoose.Schema; 

var UserSchema = new Schema({ 
    name: String, 
    email: String, 
    password: String, 
    info: String 
}); 

var User = module.exports = mongoose.model('User', UserSchema); 

module.exports.createUser = function(newUser, callback){ 
    bcrypt.genSalt(10, function(err, salt) { 
     bcrypt.hash(newUser.password, salt, function(err, hash) { 
      newUser.password = hash; 
      newUser.save(callback); 
     }); 
    }); 
} 

module.exports.getUserByUsername = function(username, callback){ 
    User.findOne({username : username}, callback); 
} 

module.exports.getUserById = function(id, callback){ 
    User.findById(id, callback); 
} 

module.exports.checkPassword = function(candidatePass, hash, callback){ 
    bcrypt.compare(candidatePass, hash, function(err, res) { 
    if(err) throw err; 
    callback(null, res); 
    }); 
} 

我的路由文件夹

//Mongoose Setup 
var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
mongoose.connect("MY_DB"); 
var path = require('path'); 
var appDir = path.dirname(require.main.filename); 
var bodyParser = require('body-parser') 
var User = require('../models/user.js'); 

//Express Setup 
var express = require('express'); 
var router = express.Router(); 
var app = express(); 
var expressValidator = require("express-validator"); 

app.use(bodyParser.urlencoded({ extended: false })); 
app.use(expressValidator()); 
app.use(bodyParser.json()); 

//Routes 
router.get('/register', function(req, res){ 
    res.sendFile(appDir + "/views/register.html"); 
}) 

router.post('/register', function(req, res) { 
    req.check('name', 'Name must be Filled in').notEmpty(); 
    req.check('email', 'Email must be Filled in').notEmpty(); 
    req.check('email', "Invalid Email").isEmail(); 
    req.check('password', 'Password Field must be Filled in').notEmpty(); 
    req.check('password', 'Passwords do not Match').equals(req.body.password2) 
    var errors = req.validationErrors(); 
    if(errors) res.send(errors) 
    else{ User.createUser({ 
    name: req.body.name, 
    email: req.body.email, 
    password: req.body.password, 
    info: req.body.user_bio 
    }, function(){ 
    console.log('User Created'); 
    }) 
} 
}) 

//Exports 
module.exports = router; 

回答

3

createUser()内users.js是一个正规的函数,你正在通过一个规则ř对象(作为newUser参数)到:

User.createUser({ 
    name : req.body.name, 
    ... 
}, ...); 

普通对象没有一个.save方法。

可能想要创建一个static method作为您的模型的一部分。这将使你打电话User.createUser像你现在正在做(注意如何静态方法对架构,而不是模型创建的。此外,你必须之前定义静态方法创建从架构模型)

+0

我在编写我的代码时引用了https://github.com/bradtraversy/loginapp/blob/master/models/user.js。为什么这个工作和我的工作不是? –

+0

查看[该代码如何使用](https://github.com/bradtraversy/loginapp/blob/master/routes/users.js#L41-L51)。它首先实例化一个新用户,并将其传递给'createUser'。但是,从代码角度来看,这个项目看起来并不是那么好。首先,它不使用Mongoose提供的工具(就像我在我的答案中提到的静态方法)。 – robertklep

4

这里有一些错误。

像这样的东西(用户是指您的模式):

var user = new User(); 
user.name = req.body.name; 
user.email = req.body.email; 
user.password = req.body.password; 
user.info = req.body.user_bio; 
user.save().then(function(err, result) { 
    console.log('User Created'); 
}); 

应该更好地工作。不是传递一个新对象(显然不包含save方法),现在您正在从模式创建一个新对象,设置参数,然后保存它。

然后,您还必须改成这样:

User.pre('save', function(next) { 
    bcrypt.genSalt(10, function(err, salt) { 
     bcrypt.hash(this.password, salt, function(err, hash) { 
      this.password = hash; 
      next(); 
     }); 
    }); 
} 

这是一个钩,这就是所谓每次用户会保存之前的时间。

+0

我将挂钩放入哪个文件? –

+0

您的架构。 user.js在你的情况。顺便说一句,可能是UserSchema.pre而不是User.pre。 –

+0

我想出了一个不同的方式来使它工作,这是非常简单的,谢谢你试图帮助。我只需要在调用createUser之前创建新用户,而不是一次完成所有操作 –

相关问题