javascript - Why is the finallyDecrypt method in this code undefined? -


my app receives base64 encoded value encrypted. data can come in few different ways wanted create chain-able methods keep code clean , modular.

i want able write: decryptionchain.decodebase64(b64value).stringtobuffer().finallydecrypt();

when run code, last property method "finallydecrypt" returns undefined.

why "finallydecrypt" method coming undefined? rest works , if run encryptionchain.decodebase64(b64value).stringtobuffer() buffer expect. when finallydecrypt chained in error out.

here code:

   function decrypt(encrypted) {     var decipher = crypto.createdecipheriv(algorithm, password, iv);     decipher.setauthtag(encrypted.tag);     var dec = decipher.update(encrypted.content, 'hex', 'utf8');     dec += decipher.final('utf8');     return dec; }  var decryptionchain = {      currentvalue:"",      decodebase64: function (encryptedvalue){         this.currentvalue = new buffer(encryptedvalue.tostring(), "base64");         return this;     },      stringtobuffer: function() {         if (this.currentvalue) {             mybuffer = json.parse(this.currentvalue, function (key, value) {                  return value && value.type === 'buffer'                     ? new buffer(value.data)                     : value;              });          }         return mybuffer;     },      finallydecrypt : function(mybuffer){         if(mybuffer){         decrypt(mybuffer);         }         return this;      } }; 

chaining works returning this each method (which points decryptionchain object).

stringtobuffer, however, returns mybuffer, you're trying call method named finallydecrypt on buffer (which doesn't have method, hence error).

if want work chaining, use similar how you're dealing currentvalue:

stringtobuffer : function() {   ...   this.mybuffer = mybuffer;   return this; }, finallydecrypt : function() {   if (this.mybuffer) {     ...   } } 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -