오류: 데이터 및 소금 인수가 필요합니다.
다음과 같이 게시 요청을 사용하여 사용자를 mongodb 데이터베이스에 저장하려고 하는데, bcrypt Error: data and hash arguments required라는 오류가 발생했습니다.코드 설정은 꽤 간단하지만 잘못된 것을 알아낼 수 없습니다. 모델/사용자.js.
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const confic = require('../models/users');
// User schema
const UserSchema = mongoose.Schema({
name: {
type: String,
},
email: {
type: String,
required: true
},
username:{
type: String,
required: true
},
password: {
type: String,
required: true
}
});
const User = module.exports = mongoose.model('User', UserSchema);
module.exports.getUserById = function(id,callback){
User.findById(id,callback);
}
module.exports.getUserByUsername = function(username,callback){
const query = {username:username}
User.findOne(query,callback);
}
module.exports.addUser= function (newUser, callback) {
bcrypt.genSalt(10,(err,salt) => {
bcrypt.hash(newUser.password, salt , (err, hash) =>{
if(err) throw (err);
newUser.password=hash;
newUser.save(callback);
});
});
}
const jwt = require('jsonwebtoken');
User = require('../models/users')
// // Register
router.post('/register', (req, res, next) => {
var newUser = new User({
name: req.body.name,
email: req.body.email,
username: req.body.username,
password: req.body.password
});
User.addUser(newUser, (err, User) => {
if(err){
// res.json({success: false, msg:'Failed to register user'});
} else {
// res.json({success: true, msg:'User registered'});
}
});
});
// Authenticate
router.post('/authenticate', (req, res, next) => {
res.send('AUTHENTICATE');
});
// Profile
router.get('/profile', (req, res, next) => {
res.send('PROFILE');
});
module.exports = router;
오류는 다음에서 발생합니다.bcrypt.hash방법.이 경우 다음 코드가 있습니다.
bcrypt.hash(newUser.password, salt , (err, hash) => { ... }
제 생각에 당신의 문제는newUser.password비어 있어야 합니다(null또는undefined) 오류는 다음과 같습니다.data and salt arguments required당신의 소금이 올바르게 생성된 것처럼 보이고 당신은 확인하지 않았습니다.newUser.password === undefined그래서 내 내기는 이렇습니다: 어떻게든.newUser.password정의되지 않았습니다.
또한, 당신은 확인할 수 있습니다.genSalt방법은 추가하면 잘 작동합니다.if(err) throw (err);당신이 했던 것처럼 그것을 부른 후.bcrypt.hash방법.
화살표 제거=>inbcrypt.hash(). 오래된 메서드 정의 사용function() {}
몽구스 문서: https://mongoosejs.com/docs/faq.html
Q. 가상, 미들웨어, 게터/세터 또는 메소드에 화살표 기능을 사용하고 있는데 이 값이 잘못되었습니다.
A. 화살표 기능은 이 키워드를 기존 기능과 크게 다르게 처리합니다.Mongoose get/setter는 사용자가 작성 중인 문서에 액세스할 수 있도록 이 기능에 의존하지만 화살표 기능에서는 작동하지 않습니다.getter/setter의 문서에 액세스하지 않을 경우 mongoose getter/setter에 화살표 기능을 사용하지 마십시오.
부동산이 공급되는 대로 있는지 확인하십시오. 저의 경우, 저는 다음을 보내고 있었습니다.Password자본금이 있는 재산P그리고 나서 통과합니다.password작게p에게 보내는 편지hash기능.
일반 텍스트 모드로 테스트할 때 이 오류가 발생했습니다(데이터 및 salt 인수 필요). 일단 json으로 변경하면 괜찮았습니다.
바디 파서 미들웨어를 설치해 보십시오.문제를 해결해야 합니다.
코드에서 사용하는 방법은 다음과 같습니다.
npmi 본문 파서 설치
app.js/server.js에서 미들웨어로 사용합니다.
const bodyparser = require("body-parser");
app.use(bodyparser.urlencoded({ extended: false }));
app.use(bodyparser.json());
언급URL : https://stackoverflow.com/questions/45015613/error-data-and-salt-arguments-required
'programing' 카테고리의 다른 글
| Angular 5에서 뷰를 렌더링하기 전에 데이터 대기 (0) | 2023.06.26 |
|---|---|
| Laravel-Echo가 Vue.js를 통해 개인 채널을 구독하지 않음 (0) | 2023.06.26 |
| Angular2 - 스타일에 [_ngcontent-mav-x] 추가 (0) | 2023.06.26 |
| Mounted hook에서 내부의 로컬 값을 저장하는 방법 - Vue 3 (0) | 2023.06.26 |
| SELECT 쿼리 성능 최적화 (0) | 2023.06.26 |