简体   繁体   English

错误:非法参数:未定义,字符串

[英]Error: Illegal arguments: undefined, string

I've been struggling with Bcrypt on my MERN project, I'm trying to run tests on Postman (registration process), but every time I try it I get this error: Error: Illegal arguments: undefined, string on this line:我一直在我的 MERN 项目中使用 Bcrypt,我试图在 Postman 上运行测试(注册过程),但每次我尝试时都会收到此错误:错误:非法参数:未定义,此行上的字符串

if (err) throw err;

This is my server main config file:这是我的服务器主配置文件:

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);

And this is the users config file where I'm getting the error:这是我收到错误的用户配置文件:

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;

this is what I see on Postman:这是我在 Postman 上看到的:

在此处输入图片说明

I've been reviewing it and it doesn't seem to have any error, and I really don't get why is telling me "undefined string".我一直在审查它,它似乎没有任何错误,我真的不明白为什么告诉我“未定义的字符串”。 If you see any error that I'm not noticing I will truly appreciate your feedback, Thanks in advance!如果您看到任何我没有注意到的错误,我将非常感谢您的反馈,提前致谢!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 _async 处的“UnhandledPromiseRejectionWarning:错误:非法 arguments:未定义,字符串” - "UnhandledPromiseRejectionWarning: Error: Illegal arguments: undefined, string" at _async 为什么此错误显示“非法 arguments:未定义,字符串”? - Why is this error displaying 'Illegal arguments: undefined, string'? 非法 arguments:未定义,字符串 - Illegal arguments: undefined, string “错误:非法 arguments:字符串,未定义”并在节点 JS 中停止服务器 - "Error: Illegal arguments: string, undefined" and stop server in node JS 尝试使用 postman API 发布数据时出现错误“非法 arguments:字符串,未定义” - Having an error " Illegal arguments: string, undefined " while trying to post data using postman API Bcrypt 错误:错误:非法回调:NodeJS 中的字符串 - Bcrypt error: Error: Illegal callback: string in NodeJS 为什么我使用简单的哈希函数出现非法参数错误? - Why I got Illegal arguments error with simple hash function? Cordova processMessage失败:堆栈:未定义(和)错误:非法访问 - Cordova processMessage failed: Stack: undefined (and) Error: illegal access 错误:未捕获的异常:[异常...“指定了无效或非法的字符串” - Error: uncaught exception: [Exception... "An invalid or illegal string was specified" 如何将大字符串分配给变量而不会出现 ILLEGAL Token 错误? - How to assign large string to a variable without ILLEGAL Token error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM