猫鼬对象方法不是一个函数

时间:2021-09-28 15:45:18

I am trying to create a route to register users for my application, but I ran into a problem. When hitting the /register route, I get the following error:

我正在尝试创建一个为我的应用程序注册用户的路由,但我遇到了一个问题。当命中/ register路由时,我收到以下错误:

TypeError: user.setPassword is not a function at 

Here is my code:

这是我的代码:

models/Users.js

var mongoose = require('mongoose');
var crypto = require('crypto');
var jwt = require('jsonwebtoken');

var UserSchema = new mongoose.Schema({
  username: {type: String, lowercase: true, unique: true},
  hash: String,
  salt: String
});

UserSchema.methods.setPassword = function(password){
  this.salt = crypto.randomBytes(16).toString('hex');
  this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
};

UserSchema.methods.validPassword = function(password) {
  var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
  return this.hash === hash;
};

UserSchema.methods.generateJWT = function() {

  // set expiration to 60 days
  var today = new Date();
  var exp = new Date(today);
  exp.setDate(today.getDate() + 60);

  return jwt.sign({
    _id: this._id,
    username: this.username,
    exp: parseInt(exp.getTime() / 1000),
  }, 'SECRET');
};

mongoose.model('User', UserSchema);

routes/index.js

var express = require('express');
var router = express.Router();
var passport = require('passport');
var mongoose = require('mongoose'); 


var User = mongoose.model('User');

router.post('/register', function(req, res, next){
  if(!req.body.username || !req.body.password){
    return res.status(400).json({message: 'Please fill out all fields'});
  }

  var user = new User();

  user.username = req.body.username;
  user.setPassword(req.body.password);

  user.save(function (err){
    if(err){ return next(err); }

    return res.json({token: user.generateJWT()})
  });
});

module.exports = router;

app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

//MongoDB Setup
var mongoose = require('mongoose');
require('./models/Users');
mongoose.connect('mongodb://localhost/images');

var passport = require('passport');
require('./config/passport');

var routes = require('./routes/index');
var app = express();
app.use(passport.initialize());
.
.
.
module.exports = app;

I'm fairly new to the MEAN stack, and after scouring the code for a few hours I can't see why things are going wrong.

我是MEAN堆栈的新手,经过几个小时的搜索,我看不出为什么会出错。

2 个解决方案

#1


1  

Try to do this:

尝试这样做:

models/Users.js

var mongoose = require('mongoose');
var crypto = require('crypto');
var jwt = require('jsonwebtoken');

var UserSchema = new mongoose.Schema({
  username: {type: String, lowercase: true, unique: true},
  hash: String,
  salt: String
});

UserSchema.methods.setPassword = function(password){
  this.salt = crypto.randomBytes(16).toString('hex');
  this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
};

UserSchema.methods.validPassword = function(password) {
  var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
  return this.hash === hash;
};

UserSchema.methods.generateJWT = function() {

  // set expiration to 60 days
  var today = new Date();
  var exp = new Date(today);
  exp.setDate(today.getDate() + 60);

  return jwt.sign({
    _id: this._id,
    username: this.username,
    exp: parseInt(exp.getTime() / 1000),
  }, 'SECRET');
};

// exports the user schema
module.exports = mongoose.model('User', UserSchema);

routes/index.js

var express = require('express');
var router = express.Router();
var passport = require('passport');
var mongoose = require('mongoose'); 
var User = require('models/user'); // require the user model in the correct path

// line removed
//var User = mongoose.model('User');

router.post('/register', function(req, res, next){
  if(!req.body.username || !req.body.password){
    return res.status(400).json({message: 'Please fill out all fields'});
  }

  var user = new User();

  user.username = req.body.username;
  user.setPassword(req.body.password);

  user.save(function (err){
    if(err){ return next(err); }

    return res.json({token: user.generateJWT()})
  });
});

module.exports = router;

Let me know if this works.

让我知道这个是否奏效。

#2


1  

Funny: Always make sure your files are not saved in some funky location. I had a copy of Users.js in my stylesheets/ folder and that was the copy I was working on this entire time. The copy in models/ in the meantime was full of little buggy things that were easy to spot.

有趣:始终确保您的文件不会保存在某个时髦的位置。我在stylesheets /文件夹中有一份Users.js副本,这是我一直在处理的副本。模特中的副本/同时充满了很容易发现的小虫子。

#1


1  

Try to do this:

尝试这样做:

models/Users.js

var mongoose = require('mongoose');
var crypto = require('crypto');
var jwt = require('jsonwebtoken');

var UserSchema = new mongoose.Schema({
  username: {type: String, lowercase: true, unique: true},
  hash: String,
  salt: String
});

UserSchema.methods.setPassword = function(password){
  this.salt = crypto.randomBytes(16).toString('hex');
  this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
};

UserSchema.methods.validPassword = function(password) {
  var hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64).toString('hex');
  return this.hash === hash;
};

UserSchema.methods.generateJWT = function() {

  // set expiration to 60 days
  var today = new Date();
  var exp = new Date(today);
  exp.setDate(today.getDate() + 60);

  return jwt.sign({
    _id: this._id,
    username: this.username,
    exp: parseInt(exp.getTime() / 1000),
  }, 'SECRET');
};

// exports the user schema
module.exports = mongoose.model('User', UserSchema);

routes/index.js

var express = require('express');
var router = express.Router();
var passport = require('passport');
var mongoose = require('mongoose'); 
var User = require('models/user'); // require the user model in the correct path

// line removed
//var User = mongoose.model('User');

router.post('/register', function(req, res, next){
  if(!req.body.username || !req.body.password){
    return res.status(400).json({message: 'Please fill out all fields'});
  }

  var user = new User();

  user.username = req.body.username;
  user.setPassword(req.body.password);

  user.save(function (err){
    if(err){ return next(err); }

    return res.json({token: user.generateJWT()})
  });
});

module.exports = router;

Let me know if this works.

让我知道这个是否奏效。

#2


1  

Funny: Always make sure your files are not saved in some funky location. I had a copy of Users.js in my stylesheets/ folder and that was the copy I was working on this entire time. The copy in models/ in the meantime was full of little buggy things that were easy to spot.

有趣:始终确保您的文件不会保存在某个时髦的位置。我在stylesheets /文件夹中有一份Users.js副本,这是我一直在处理的副本。模特中的副本/同时充满了很容易发现的小虫子。

相关文章