gpt4 book ai didi

node.js - Bcrypt 错误 : illegal arguments String unidentified

转载 作者:IT老高 更新时间:2023-10-28 23:27:23 25 4
gpt4 key购买 nike

这是我的完整代码

var express = require('express'),
app = express(),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
morgan = require('morgan'),
webToken = require('jsonwebtoken'),
bcrypt = require('bcryptjs'),
assert = require('assert');
Schema = mongoose.Schema,
secretKey = "omjdiuwkslxmshsoepdukslsj";

//User Schema
var userSchema = new Schema({
username: {type: String, required: true, index: {unique:true}},
password: {type: String, required: true, select: false}
})

userSchema.pre('save', function(next){
var user = this;

if(!user.isModified('password')) return next();

bcrypt.hash(user.password, null, null, function(err, hash){
if(err) return next(err);

user.password = hash;
next();
})
});

userSchema.methods.comparePassword = function(password){
var user = this;

bcrypt.compare(password, user.password, function(err, result){
if(err){
console.log(err);
}
else {
console.log("passwords match!");
return;
}
})
}

var userModel = mongoose.model('users', userSchema);


//Connecting to Mongo
mongoose.connect("mongodb://localhost/userstories", function(err){
if(err) {
console.log(err);
}
else {
console.log("Connected to database!");
}
});

//Creating Token
function createToken(user){
var token = webToken.sign({
_id: user.id,
username: user.username
}, secretKey,{
expiresIn: 30 * 60 * 1000
})
return token;
}

//Middlewares
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(morgan('dev'));

//Api's

app.post('/signup', function(req, res){
var user = new userModel({
username: req.body.username,
password: req.body.password
})

user.save(function(err){
if(err){
console.log(err);
}

else{
res.json({message: "User created!"});
}
})
})

app.post('/login', function(req, res){

userModel.findOne({username: req.body.username}, function(err, user){
if(err) console.log(err);

if(!user){
res.send("User not found!");
}
else if(user){

var validPassword = user.comparePassword(req.body.password);

if(validPassword){
var tokens = createToken(user);

res.json({
success: true,
message: "Successfully logged In",
token: tokens
});
}

else {
res.send("Invalid password");
}

}
})
});


//Running the server
app.listen(3000, function(err){
if(err) console.log("port not working");
else{
console.log("Everything went just fine");
}
})

我已经尝试了所有方法,并在这里看到了所有答案。但似乎没有人遇到过这种非法论证错误。请帮我解决这个问题,我确定有一个我看不到的错误

最佳答案

在您的用户架构中,您将密码字段的 select 设置为 false。这意味着,当您尝试在 login 请求中执行操作时,无论何时在架构中查找用户,您都不会获得 password 字段的值或在架构中定义了 select false 的任何其他字段。

你需要做的是在找到用户时指定你需要的密码:

app.post('/login', function(req, res){
userModel.findOne({username: req.body.username}, 'password', function(err, user){
// continue
}

这将只返回来自数据库的 _idpassword。如果要返回其他字段,则必须将它们添加到:

app.post('/login', function(req, res){
userModel.findOne({username: req.body.username}, 'password firstName lastName email', function(err, user){
// continue
}

关于node.js - Bcrypt 错误 : illegal arguments String unidentified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34982964/

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