我开始使用Mongo,Express和Node开发我的第一个API。当我尝试让一个特定用户使用API endponit时,控制台抛出错误ReferenceError: err is not defined
。我已经用于身份验证部分的方法中出现错误,并且可以正常工作。错误所在的代码部分在第5行:
exports.userById = (req, res, next, id) => {
User.findById(id).exec(() => {
if(err || !user) {
return res.status(400).json({
err: "User not found"
});
}
req.profile = user //adds profile object in req with user info
next();
});
}
此外,我尝试获得单个用户的代码部分:
exports.getUser = (req, res) => {
req.profile.hashed_password = undefined;
req.profile.salt = undefined;
return res.json(req.profile);
}
我认为问题可能不在这里,但是路由文件中也有路由行
router.get("/users/:userId", authController.requireSignin, userController.getUser);
感谢大家的帮助!
答案 0 :(得分:2)
我很确定import embed
来自err
:
exec
答案 1 :(得分:0)
编辑我想您想按ID搜索并返回某些内容。试试这个。
from django.contrib.auth.forms import UserCreationForm
class RegisterForm(UserCreationForm):
def clean_email(self):
email = self.cleaned_data['email']
if User.objects.filter(email=email).exists():
raise ValidationError('Email Already Exists')
return email
class Meta:
model = User
fields = ['username', "email", "password1", "password2"]