0
我是Node.js的新手,我創建了一個小型加密腳本。然而,林在訪問從一個函數的變量在另一個函數節點訪問變量外功能
var triplesec = require('triplesec');
var data = 'secretthings'
// Encrypt Function
triplesec.encrypt({
key: new triplesec.Buffer('secretkey'),
data: new triplesec.Buffer(data),
}, function (err, buff) {
if(!err) {
global.data = buff.toString('hex')
//console.log(buff.toString('hex'))
}
});
// Decrypt Function
triplesec.decrypt({
data: new triplesec.Buffer(global.data, "hex"),
key: new triplesec.Buffer('secretkey')
}, function (err, buff) {
if(!err) {
console.log(buff.toString());
}
});
問題,當我運行上面的代碼,我得到一個錯誤,指出:
buffer.js:67 拋出新的TypeError('必須從數字,緩衝區,數組或字符串開始');
我該如何做到這一點?
要麼使一些全局變量和變異,或者明確地把它作爲參數傳遞給函數調用。 –
將整個'triplesec.decrypt'函數調用移到定義了'ciphertext'的'if'語句的末尾,因爲它只在該塊中可用。 –
我使它成爲一個全球性的,現在我得到一個錯誤,說:'buffer.js:67 扔新TypeError('必須以數字,緩衝區,數組或字符串開始');'' – bshah