Node.js 我的注册路径有错误
在测试过程中,如果我的用户的注册路径使用postman作为测试机器运行良好,它会抛出一个错误,我不知道这意味着什么 我曾尝试使用异步函数捕捉错误,但没有成功Node.js 我的注册路径有错误,node.js,express,routes,postman,Node.js,Express,Routes,Postman,在测试过程中,如果我的用户的注册路径使用postman作为测试机器运行良好,它会抛出一个错误,我不知道这意味着什么 我曾尝试使用异步函数捕捉错误,但没有成功 const express = require('express'); const router = express.Router(); // Use to help set a default image for users const gravatar = require('gravatar'); // Use to encrypt o
const express = require('express');
const router = express.Router();
// Use to help set a default image for users
const gravatar = require('gravatar');
// Use to encrypt our password from bringing plain text
const bcrypt = require('bcryptjs');
// I add the user model from my model
// So i can be able to check create a new registration
// and also check if email exist
const User = require('../../models/User');
// @route GET api/users/register
// @desc Register user
// @access Public
router.post('/register', (req, res) => {
User.findOne({ email: req.body.email }).then(user => {
if (user) {
errors.email = 'Email already exists';
return res.status(400).json(errors);
} else {
const avatar = gravatar.url(req.body.email, {
s: '200', // Size
r: 'pg', // Rating
d: 'mm' // Default
});
const newUser = new User({
name: req.body.name,
email: req.body.email,
avatar,
password: req.body.password
});
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) throw err;
newUser.password = hash;
newUser
.save()
.then(user => res.json(user))
.catch(err => console.log(err));
});
});
}
});
});
module.exports = router;
我希望用户输入的邮递员应该能够张贴的形式,这样我就可以知道,如果路线实际工作良好。这就是我控制台上的错误
(node:14164) UnhandledPromiseRejectionWarning: ReferenceError: errors is not defined
at User.findOne.then.user (the working director/name.js:26:7)
at process._tickCallback (internal/process/next_tick.js:68:7)
(node:14164) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:14164) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
通常,我们应以以下方式处理错误:
User.findOne({ email: req.body.email }).then(user => {
// ...
}).catch(error => {
console.log('error', error)
res.json({error: error})
})
通常,我们应以以下方式处理错误:
User.findOne({ email: req.body.email }).then(user => {
// ...
}).catch(error => {
console.log('error', error)
res.json({error: error})
})
错误
未定义,您应该处理承诺拒绝。您能解释一下此函数在哪里声明吗User.findOne()
,我的意思是,这不是MongoDB吗?错误
没有定义,你应该处理承诺拒绝。你能解释一下这个函数是在哪里声明的吗User.findOne()
,我是说,那不是MongoDB吗?