gpt4 book ai didi

javascript - 错误 : Illegal arguments: undefined, 字符串

转载 作者:行者123 更新时间:2023-11-30 20:09:06 28 4
gpt4 key购买 nike

我一直在为我的 MERN 项目使用 Bcrypt 而苦苦挣扎,我试图在 Postman 上运行测试(注册过程),但每次尝试时我都会收到此错误:错误:非法参数:未定义,此行上的字符串:

if (err) throw err;

这是我的服务器主配置文件:

const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');

const users = require('./routes/api/users');
const profile = require('./routes/api/profile');
const posts = require('./routes/api/posts');

const app = express();

// Body parser Middleware
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());

// MongoDB Config
const db = require('./config/keys').mongoURI;

// MongoDB Connection
mongoose
.connect(db)
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log(err));

app.get('/', (req, res) => res.send('Hello!'));

const port = process.env.PORT || 3000;

app.listen(port, () => console.log(`Server Runing on port ${port}`));

// Using Routes

app.use('/api/users', users);
app.use('/api/profile', profile);
app.use('/api/posts', posts);

这是我收到错误的用户配置文件:

const express = require('express');
const router = express.Router();
const gravatar = require('gravatar');
const bcrypt = require('bcryptjs');

// Load user model:
const User = require('../../models/User');

router.get('/test', (req, res) => res.json({ msg: "Users Works" }));

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', // Size
r: 'pg', // Rating
d: 'mm' // Default image
}));

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;

这是我在 Postman 上看到的:

enter image description here

我一直在审查它,它似乎没有任何错误,我真的不明白为什么告诉我“未定义的字符串”。如果您看到任何我没有注意到的错误,我将非常感谢您的反馈,提前致谢!

最佳答案

   const newUser = new User({
name: req.body.name,
email: req.body.email,
avatar, <------------------------- avatar:'', or avatar:null,
password: req.body.password
});

关于javascript - 错误 : Illegal arguments: undefined, 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52586294/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com