javascript - 邮递员:TypeError:User.findOne 不是函数
问题描述
邮递员:TypeError:User.findOne
不是函数
嗨,我正在尝试在讲座结束时运行 POST 请求,但出现此错误。
我认为问题出在User.findOne
方法上。
我的代码如下:
//users.js
const express = require('express');
const router = express.Router();
const gravatar = require('gravatar');
const bcrypt = require('bcryptjs');
// Load User Model
const User = require('../../models/User');
//@route GET api/users/test
//@description Test users route
//@access Public
router.get('/test', (req, res) => res.json({
msg: 'Users Works'
}));
//@route GET api/users/register
//@description Register a user
//@access Public
router.post('/register', (req, res) => {
User.findOne({
email: req.body.email
}).then(user => {
if (user) {
return res.status(400).json({
email: 'Email already exists'
});
} else {
const avatar = gravatar.url(req.body.email, {
s: '200',
r: 'pg',
d: 'mm'
});
const newUser = new User({
name: req.body.name,
email: req.body.email,
avatar,
password: req.body.password
});
// encryption of password, throw and catch of error
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
解决方案
推荐阅读
- android - 什么是更快的通信,直接在Android中发送广播VS调用活动方法?
- angular - 动画启动画面 - Ionic 4.0
- odbc - Tableau Linux 服务器,它是否无法识别用户 ODBC 设置
- python - python - 如何使用pandas更新而不是重写csv来获取股票数据?
- iis - Windows Server 2016 IIS 托管网站无法在阿里云上的远程文件共享上加载 dll
- mysql - JOIN 顺序和 WHERE
- nativescript - 滚动时触发滚动视图内的点击事件
- javascript - pop 函数没有从蛇数组中取出尾巴?
- python - 我想解决 Django NoReverseMatch
- git - 运行`post-checkout`钩子时如何仅影响更改的文件夹和文件?