.save()不是函数Mongoose [英] .save() is not a Function Mongoose

查看:142
本文介绍了.save()不是函数Mongoose的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I keep getting an issue that newUser.save() is not a function. This is a mongoose function that I have used before. I required mongoose correctly and am unsure of why this error is occurring. Any help is welcomed.

我得到的错误是 TypeError:newUser.save不是函数

我在模型文件夹中的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);
  });
}

路由文件夹中的我的users.js

//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;


推荐答案

createUser()是一个常规函数,您将常规对象(作为 newUser 参数)传递给:

createUser() is a regular function that you're passing a regular object (as the newUser argument) to:

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

常规对象没有 .save 方法。

可能想要的是创建一个静态方法作为模型的一部分。这将允许您像现在一样调用 User.createUser (注意如何在架构上创建静态方法,而不是模型。此外,您必须在从模式创建模型之前定义静态方法

What you probably want is to create a static method as part of your model. That would allow you to call User.createUser like you are doing now (notice how static methods are created on the schema, not the model. Also, you have to define static methods before creating a model from the schema)

这篇关于.save()不是函数Mongoose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆