gpt4 book ai didi

javascript - newUser.some 函数不是函数 [Mongoose]

转载 作者:行者123 更新时间:2023-12-03 07:35:27 25 4
gpt4 key购买 nike

我进行了简单的注册并向数据库添加了一个函数,当我尝试使用它时,我收到此错误 - TypeError: newUser.generateHash is not a function。我是护照新手,我想只使用护照登录。我已经禁用了路由来简化问题。

用户.js

    var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');

var userSchema = mongoose.Schema({
username: String,
password: String
});

var pollsSchema = mongoose.Schema({
pollTitle: String,
option1: String,
option2: String,
option3: String,
option4: String
})

userSchema.methods.generateHash = function generateHash(password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
}

userSchema.methods.validPassword = function validPassword (password) {
return bcrypt.compareSync(password, this.password);
}


module.exports = mongoose.model('User', userSchema);
module.exports = mongoose.model('polls', pollsSchema);

服务器.js

var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser')
var mongoose = require('mongoose');
var session = require('express-session')
var passport = require('passport');
var LocalStrategy = require('passport-local');
var flash = require('connect-flash');
var bcrypt = require('bcrypt-nodejs');
var User = require('./models/user');

var app = express();
//Connection to Database
mongoose.connect('mongodb://localhost/polls');

//Post for enviorement
var port = process.env.PORT || 9000;

//Using middleware
app.use(session({
secret: 'secretstuff',
resave: true,
saveUninitialized: true
}));

app.use(express.static(path.join(__dirname, '/tmp')));
app.use(express.static(path.join(__dirname, '/bower_components')));
app.use(cookieParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());

//require('./config/passport')(passport);
//configuration of app

app.set('view engine', 'jade');
app.set('views', path.join(__dirname, 'views'));

//Routes
//require('./routes/dynamic.js')(app, passport);
app.get('/', function(req, res) {
res.render('index');
});


app.post('/signup', function(req, res) {
console.log('sdsss');
var pattUser = /^[a-z\S]{6,12}$/;
var pattPass = /^[a-z\S]{8,14}$/;
var username = req.body.username;
var pass = req.body.password;
var confirmpass = req.body.confirmpass;
if(pattUser.test(username) === true && pattPass.test(pass) === true && pass === confirmpass) {
console.log('ddddd-server');
User.findOne({username: username}, function(err, user) {
if(err) { return err; }
if(user) {
res.jsonp({message: 'username already taken'});
} else {
var newUser = new User();
console.log(newUser.generateHash(pass));
newUser.username = username;
newUser.password = newUser.generateHash(pass);

newUser.save(function(err) {
if(err) { return err; }
})
res.jsonp({message: 'You can now Log In!'});
}
})
}
});

app.listen(port, function() {
console.log('Our app is running on http://localhost:' + port);
});

最佳答案

您不能多次使用“module.exports =”。第二条语句覆盖第一条语句。如果您想导出两个不同的架构,请执行以下操作:

module.exports.User = mongoose.model('User', userSchema);
module.exports.Polls= mongoose.model('polls', pollsSchema);

然后在你的server.js

var schemas = require('./models/user');
var User = schemas.User;
var polls = schemas.Polls;

关于javascript - newUser.some 函数不是函数 [Mongoose],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35630439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com