crypto как получить зашифрованную переменную и добавить в обект
пишет ошибку:
TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer. Received type number (9)
at validChunk (_stream_writable.js:281:10)
at Cipheriv.Writable.write (_stream_writable.js:316:21)
at Query.<anonymous> (C:\xampp\htdocs\mysite.local\app.js:235:26)
at Query.<anonymous> (C:\xampp\htdocs\mysite.local\node_modules\mysql\l ib\Connection.js:526:10)
at Query._callback (C:\xampp\htdocs\mysite.local\node_modules\mysql\l ib\Connection.js:488:16)
at Query.Sequence.end (C:\xampp\htdocs\mysite.local\node_modules\mysql\l ib\protocol\sequences\Sequence.js:83:24)
at Query._handleFinalResultPacket (C:\xampp\htdocs\mysite.local\node_modules\mysql\l ib\protocol\sequences\Query.js:149:8)
at Query.EofPacket (C:\xampp\htdocs\mysite.local\node_modules\mysql\l ib\protocol\sequences\Query.js:133:8)
at Protocol._parsePacket (C:\xampp\htdocs\mysite.local\node_modules\mysql\l ib\protocol\Protocol.js:291:23)
at Parser._parsePacket (C:\xampp\htdocs\mysite.local\node_modules\mysql\l ib\protocol\Parser.js:433:10)
Emitted 'error' event on Cipheriv instance at:
at errorOrDestroy (internal/streams/destroy.js:108:12)
at validChunk (_stream_writable.js:284:5)
at Cipheriv.Writable.write (_stream_writable.js:316:21)
[... lines matching original stack trace ...]
at Protocol._parsePacket (C:\xampp\htdocs\mysite.local\node_modules\mysql\l ib\protocol\Protocol.js:291:23) {
code: 'ERR_INVALID_ARG_TYPE'
}
connection.connect();
query='SELECT `photo`,`name`,`price`,`description`,`sale`,`id`,(`price` -`price` / 100 * `sale`) AS `tsale` FROM `smartphone` '+where ;
connection.query(query, function(err, rows, fields) {
if (err) {
res.status(500).json({"status_code": 500,"status_message": "internal server error"});
console.log(err);
} else {
// Loop check on each row
var rowlenght=rows.length+1;
for (var i = 0; i < rows.length; i++) {
var cipherStream = crypto.createCipheriv('aes-256-cbc', key, iv);
var encryptedData = [];
cipherStream.on('data', data => encryptedData[i]['id']= data.toString('hex'));
cipherStream.write(rows[i].id);
cipherStream.end();
var idh= encryptedDataх[i]['id'];
// Create an object to save current row's data
var smart = {
'id': rows[i].id,
'idh': idh,
'name': rows[i].name,
//'nameh': nameh,
'photo':rows[i].photo,
//'photoh':photoh,
'price': rows[i].price,
//'priceh': priceh,
'sale': rows[i].sale,
//'saleh':saleh,
'tsale': (rows[i].price-rows[i].price*rows[i].sale),
'description': rows[i].description,
};
// Add object into array
smartphopne.push(smart);
}
res.render('index', {"smartphopne": smartphopne,});
}
});