"ReferenceError: userData is not defined" - How to define userData in separate, helpers function?
"ReferenceError: userData is not defined" - How to define userData in separate, helpers function?
我是初学者。我想将加密函数 (encryption.js) 从 authentication.js 文件中分离出来,但我收到错误“ReferenceError: userData is not defined”。如何在单独的辅助函数中定义 userData?
authentiocation.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:
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
}
您可以将其作为参数传递给加密函数(在 encryption.js
文件中):
function encryption(userData) {
...
// at the end:
return encrypted;
}
然后在authentiocation.js
中这样调用(你是说authentication吗?):
const encrypted = encryption(userData);
注:
编辑以解决评论中提到的其他问题。
我是初学者。我想将加密函数 (encryption.js) 从 authentication.js 文件中分离出来,但我收到错误“ReferenceError: userData is not defined”。如何在单独的辅助函数中定义 userData?
authentiocation.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:
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
}
您可以将其作为参数传递给加密函数(在 encryption.js
文件中):
function encryption(userData) {
...
// at the end:
return encrypted;
}
然后在authentiocation.js
中这样调用(你是说authentication吗?):
const encrypted = encryption(userData);
注:
编辑以解决评论中提到的其他问题。