我可以在我的网站上注册用户,但当我用Postman测试它时,我得到了这个错误:
MissingPasswordError: No password given
我也不能登录,因为密码/用户名组合不正确,所以肯定有问题,但我无论如何也找不出是什么问题。
这是我输入密码的html输入域:
<input type="password" class="input--text" name="password" id="password">
使用我的fetch函数的signup.js
:
const btnSignup = document.querySelector('.btn').addEventListener('click', function () {
let username = document.querySelector('#username').value;
let password = document.querySelector('#password').value;
let bday = document.querySelector('#bday').value;
fetch('http://localhost:3000/users/signup', {
method: "post",
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'username': username,
'password': password,
'bday': bday
})
}).then(response => {
return response.json();
}).then(json => {
if (json.status === 'success') {
let feedback = document.querySelector('.alert');
feedback.textContent = "Sign up successful!";
feedback.classList.remove('hidden');
}
})
})
这是我在auth.js
控制器中的注册函数:
const signup = async (req, res, next) => {
const username = req.body.username;
const password = req.body.password;
const bday = req.body.bday;
const user = new Users({
username: username
});
/*user.bday = bday;*/
await user.setPassword(password);
await user.save()
.then(result => {
console.log(result.id);
let token = jwt.sign({
id: result._id
}, "{this is a secret}");
res.json({
'status': 'success',
'data': {
'token': token
}
})
}).catch(error => {
res.json({
'status': 'error'
})
});
}
我添加了bodyParser,确保输入字段的名称是正确的,...
发布于 2020-08-31 14:35:22
解决了!事实证明我的代码是正确的,但是我忘记了将Postman中的原始正文设置为JSON (默认设置为text)。
https://stackoverflow.com/questions/63669677
复制相似问题