Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript &引用;ReferenceError:未定义用户数据";-如何在单独的helpers函数中定义userData?_Javascript_Node.js_Function_Backend_User Data - Fatal编程技术网

Javascript &引用;ReferenceError:未定义用户数据";-如何在单独的helpers函数中定义userData?

Javascript &引用;ReferenceError:未定义用户数据";-如何在单独的helpers函数中定义userData?,javascript,node.js,function,backend,user-data,Javascript,Node.js,Function,Backend,User Data,我是初学者。我想将加密函数(encryption.js)从authentication.js文件中分离出来,但得到一个错误“ReferenceError:userData未定义”。如何在单独的helpers函数中定义userData authentication.js: const express = require('express'); const router = express.Router(); const User = require('../models/user'); const

我是初学者。我想将加密函数(encryption.js)从authentication.js文件中分离出来,但得到一个错误“ReferenceError:userData未定义”。如何在单独的helpers函数中定义userData

authentication.js:

const express = require('express');
const router = express.Router();
const User = require('../models/user');
const jwt = require('jsonwebtoken');
const encryption = require("../helpers/encryption").encryption;

router.post('/register', (req, res) => { // rejestracja
  const userData = req.body;

  User.findOne({ email: userData.email }, (error, user) => {
   (...)
      else {
        
        encryption(); // <-----------------

        const user = new User({
          firstname: userData.firstname,
          email: userData.email,
          surname: userData.surname,
          password: encrypted
        });
        user.save((error, registeredUser) => {
          if (error) {
            res.status(401).send('Błąd rejestracji!')
          } else {
            const firstname = user.firstname;
            surname = user.surname;
            email = user.email;
            payload = { subject: registeredUser._id };
            token = jwt.sign(payload, 'secretKey');
            res.status(200).send({ token, firstname, surname, email });
          }

您可以将其作为参数传递给加密函数(在
encryption.js
文件中):

然后在
authentication.js
中这样调用它(你这里是指身份验证吗?)

注:
编辑以解决评论中提到的附加问题。

加密(用户数据)
函数加密(userData){
谢谢,它可以工作:)非常感谢!我不知道为什么我没有想到:)但是我仍然有一个问题,这次是authentication.js中的“encrypted”,因为那里没有定义,我得到一个错误“ReferenceError:encrypted is not defined”。你知道如何修复它吗(哇,太好了,非常感谢你的帮助。现在一切都正常了。:)很高兴听到这个消息&祝你作为程序员的旅途好运
const express = require('express');

function encryption() {
    const crypto = require('crypto');
    const algorithm = 'aes-192-cbc';
    // Key length is dependent on the algorithm. In this case for aes192, it is
    // 24 bytes (192 bits).
    // Use async `crypto.scrypt()` instead.
    const key = crypto.scryptSync(userData.password, 'salt', 24);
    // Use `crypto.randomBytes()` to generate a random iv instead of the static iv
    // shown here.
    const iv = Buffer.alloc(16, 0); // Initialization vector.
    const cipher = crypto.createCipheriv(algorithm, key, iv);
    let encrypted = '';
    cipher.on('readable', () => {
        let chunk;
        while (null !== (chunk = cipher.read())) {
            encrypted += chunk.toString('hex');
        }
    });

    cipher.write('some clear text data');
    cipher.end();
}

module.exports = {
    "encryption": encryption
}
function encryption(userData) {
    ...
    // at the end:
    return encrypted;
}
const encrypted = encryption(userData);