NodeJS 為什麼不在我的 IF/Else 語句中調用我的函數

貝托

我有一個關於節點和在 if/else 語句中調用函數的問題。下面是我的情況。我不確定為什麼從第一個 IF 語句調用 awsCreds 函數會起作用,但是當我嘗試從 else 語句調用它時它不起作用。它是否與我在 else 語句中有一個 if/else 的事實有關?

let cacheExpiration;
let currentTime;
let awsCredentials;

async function stsGetCallerIdentity(creds, message) {
   const stsParams = { credentials: creds };
   // Create STS service object
   const kinesis = new AWS.Kinesis(stsParams);
   const parsed_value = JSON.parse(message);
   const type = parsed_value.type;
   console.info(type);

   const message_type = {
      "transcript": process.env.TRANSCRIPT_KINESIS,
      "tone": process.env.TONE_KINESIS,
      "pitch": process.env.PITCH_KINESIS
   };
   const stream_name = message_type[type];
   console.info(stream_name);

   const params = {
      Data: Buffer.from(message),
      PartitionKey: `1111`,
      StreamName: stream_name
   };
   kinesis.putRecord(params, function(err, data) {
      if (err) console.error(err);
      else {
         console.info(JSON.stringify(data));
      }
   });
}

function awsCreds() {
   return new Promise((resolve, reject) => {
      const roleToAssume = {
         RoleArn: process.env.IAMRole,
         RoleSessionName: `session1`,
         DurationSeconds: 900 };

      sts.assumeRole(roleToAssume, function(err, data) {
         if (err) {
            console.error(err, err.stack);
            reject(err.stack);
         } else {
            awsCreds = {
               accessKeyId: data.Credentials.AccessKeyId,
               secretAccessKey: data.Credentials.SecretAccessKey,
               sessionToken: data.Credentials.SessionToken,
               cacheExpiration: data.Credentials.Expiration
            };
            resolve(awsCreds);
         }
      });
   });
};

webSocketClient.on(`message`, async (message) => {
try {
  currentTime = new Date();
  console.log(`currentTime`, currentTime);
  console.log(`cacheExpiration`, cacheExpiration);


          if (cacheExpiration == undefined) {
            //Calling awsCreds returns values.
           awsCredentials = await awsCreds();
           stsGetCallerIdentity(awsCredentials, message)
    
        } else {
           const minutes = (cacheExpiration.getTime() - currentTime.getTime());
           console.log(`minutes`, minutes);
           if (minutes > 60000) {
              //Do something
           stsGetCallerIdentity(awsCredentials, message)
           } else {
              
              // Calling awsCreds from here returns error 'awsCreds is      not a function'.
console.log(`Refreshing credentials`);
              awsCredentials = await awsCreds();
              stsGetCallerIdentity(awsCredentials, message)
           }
        }
       
     } catch (error) {
        console.error(`There was an error`, error);
     };
)};
勞倫斯·切羅內

你的函數被調用awsCreds,然後你awsCreds在你之前分配為一個對象文字resolve,你需要const在前面,或者根本不分配只是把對象放在解析中:

改變

awsCreds = {
   accessKeyId: data.Credentials.AccessKeyId,
   secretAccessKey: data.Credentials.SecretAccessKey,
   sessionToken: data.Credentials.SessionToken,
   cacheExpiration: data.Credentials.Expiration
};
resolve(awsCreds);

resolve({
   accessKeyId: data.Credentials.AccessKeyId,
   secretAccessKey: data.Credentials.SecretAccessKey,
   sessionToken: data.Credentials.SessionToken,
   cacheExpiration: data.Credentials.Expiration
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

NodeJS 在編譯時似乎運行了一個導出的函數——為什麼?

為什麼我的 javascript reduce 函數被跳過了?

為什麼我的第三個元素不在其父元素之外?

為什麼我的評級是反向的?

為什麼我不能在 tkinter 綁定中調用函數?

為什麼我無法使用 NodeJS 連接到 SQLServer?

為什麼我的函數 encodeChar 不起作用?

誰能告訴我為什麼這個函數在調用時會拋出錯誤

誰能告訴我為什麼在嘗試調用此函數時會出錯?

為什麼這個簡單的導出在 nodejs 中給了我錯誤?

那麼,為什麼我必須在基類中定義虛函數?

為什麼我的列表不在帖子之間保持 PageModel 上的狀態?

為什麼調用 someMockInstance.Object 調用構造函數?

為什麼我的 C 數組會記住之前函數調用的舊數據?

為什麼我的函數在 componentDidMount 之前運行

為什麼我不能在函數中設置我的構造函數?

為什麼我不能調用 WebComponent 的方法?

為什麼我得到 QJsonValue(undefined)?

為什麼我的 onsubmit 驗證函數沒有提交我的函數?

為什麼我的數據在異步函數 nodejs 打字稿中更新之前返回狀態

為什麼我的 IFS 函數不適用於 QUERY?

為什麼我的代碼調用一個函數兩次?

為什麼我收到 403 - 禁止

為什麼#undef 不適用於我的函數?

為什麼我的 div 塊不在同一個地方?

從多列創建變量的 ifelse 語句不起作用,我不知道為什麼

為什麼我得到的值超出了我的 rand 函數參數?

為什麼我的變量只在 Python 中的某個函數/語句中發生變化?

為什麼 NodeJS 將數字視為整數?