从.env导入变量-Express Cloud函数-显示未定义

时间:2020-10-23 19:44:41

标签: express environment-variables

.env

'exposed_headers' => ['Authorization'],

index.ts

AES_SECRET= 123

错误-对象可能是'undefined'。 秘密显示为未定义

2 个答案:

答案 0 :(得分:2)

尝试这样:

dotenv.config();
const secret = <string>process.env.AES_SECRET;
.
.
const decrypt = (hash: string) => {
 var reb64 = CryptoJS.enc.Hex.parse(hash);
  var bytes = reb64.toString(CryptoJS.enc.Base64);
  var decrypt;
  
  if (secret !== undefined) {
     decrypt = AES.decrypt(bytes, secret);
  }
  
  var plainText = decrypt.toString(CryptoJS.enc.Utf8);

  return plainText
}

答案 1 :(得分:0)

已更新: 我最终这样做了

const presecret = process.env.AES_SECRET;
const secret = presecret!.toString();