AWS Lambda 函數全局變量 (AWS Lambda function global variables)


問題描述

AWS Lambda 函數全局變量 (AWS Lambda function global variables)

我正在用 JavaScript (Node.js) 編寫一個 AWS Lambda 函數,該函數通過 AWS 開發工具包與 CodeCommit 交互。

服務之間的通信按預期工作,我正在 CodeCommit 函數中獲取數據,但是當我想在函數之外使用這些數據時,就會出現我面臨的問題。

我嘗試了兩種方法:

1。全局變量

代碼:

var aws = require('aws‑sdk');
var codecommit = new aws.CodeCommit({ apiVersion: '2015‑04‑13' });
var repoName = ''; // Declared my global variable here

exports.handler = function(event, context) {

    var commitId = "69a5f8eeba340d71ba41b8f20d77cc20b301ff52"
    var repository = "my‑repository"

    var params = {
        repositoryName: repository
    };
    codecommit.getRepository(params, function(err, data) {
        if (err) {
            console.log(err);
            var message = "Error getting repository metadata for repository " + repository;
            console.log(message);
            context.fail(message);
        } else {
            console.log('Repository Name:', data.repositoryMetadata.repositoryName); // Shown with data
            repoName = data.repositoryMetadata.repositoryName; // Setting the variable
            console.log('Account Id:', data.repositoryMetadata.accountId); // Shown with data
        }
    });

    console.log(repoName); // Shown as blank in the output
};

輸出:
最後寫入的“console.log” 是第一個打印在執行結果中,但顯示為空白。然後打印另外兩個console.log(在函數內),並顯示數據。

2。函數

代碼:

var aws = require('aws‑sdk');
var codecommit = new aws.CodeCommit({ apiVersion: '2015‑04‑13' });

exports.handler = function(event, context) {

    var commitId = "69a5f8eeba340d71ba41b8f20d77cc20b301ff52"
    var repository = "my‑repository"

    var repoData = getRepository(repository)

    console.log('Repository Name:', repoData.repositoryName);
    console.log('Account Id:', repoData.accountId);
};

function getRepository(repository) {
    var params = {
        repositoryName: repository
    };
    codecommit.getRepository(params, function(err, data) {
        if (err) {
            console.log(err);
            var message = "Error getting repository metadata for repository " + repository;
            console.log(message);
            context.fail(message);
        } else {
            var repoData = {};
            repoData.repositoryName = data.repositoryMetadata.repositoryName;
            repoData.accountId = data.repositoryMetadata.accountId;
            console.log(repoData); // Shows output in execution results when lines 11 & 12 are commented
            return repoData;
        }
    });
}

輸出:

{
  "errorType": "TypeError",
  "errorMessage": "Cannot read property 'repositoryName' of undefined",
  "trace": [
    "TypeError: Cannot read property 'repositoryName' of undefined",
    "    at Runtime.exports.handler (/var/task/index.js:57:46)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
  ]
}

結論
這些方法都不起作用。數據始終在函數內可見,但從不在函數外。我懷疑函數外部的代碼在函數本身之前執行,我想知道我是否可以讓代碼在執行 console.log 之前等待函數已經執行(以及之後的其他操作)。或者也許我在另一個層面上錯了?

日誌(以及之後的其他操作)。或者也許我在另一個層面上錯了?

日誌(以及之後的其他操作)。或者也許我在另一個層面上錯了?


參考解法

方法 1:

You are using a callback model, in which case the console.log in the first example is being hit before the code in the callback. A better option would be to use async/await.

var aws = require('aws‑sdk');
var codecommit = new aws.CodeCommit({ apiVersion: '2015‑04‑13' });
var repoName = ''; // Declared my global variable here

exports.handler = async function(event, context) {

var commitId = "69a5f8eeba340d71ba41b8f20d77cc20b301ff52"
var repository = "my‑repository"

var params = {
    repositoryName: repository
};
var data = await codecommit.getRepository(params).promise();

console.log('Repository Name:', data.repositoryMetadata.repositoryName); // Shown with data
repoName = data.repositoryMetadata.repositoryName; // Setting the variable
console.log('Account Id:', data.repositoryMetadata.accountId); // Shown with data

console.log(repoName);

};
</code></pre>

Notice that I'm not catching the error here, but if you wanted to you can use a try/catch block. Just be sure you throw a new error in that case if you want the function to fail.

(by GuillaumeExiaJason Wadsworth)

參考文件

  1. AWS Lambda function global variables (CC BY‑SA 2.5/3.0/4.0)

#javascript #serverless #amazon-web-services #node.js #aws-lambda






相關問題

為什麼我不能在 IE8 (javascript) 上擴展 localStorage? (Why can't I extend localStorage on IE8 (javascript)?)

在 Javascript 中打開外部 sqlite3 數據庫 (Open external sqlite3 database in Javascript)

Javascript:數組中的所有對像都具有相同的屬性 (Javascript: All Objects in Array Have Same Properties)

為什麼我們要在 javascripts 原型中添加函數? (Why do we add functions to javascripts prototype?)

顯示 URL javascript 的最後一部分? (Display the last part of URL javascript?)

Javascript XMLHttpRequest:忽略無效的 SSL 證書 (Javascript XMLHttpRequest: Ignore invalid SSL Certificate)

有沒有辦法測試 console.log 整體 (Is there a way to test for console.log entires)

如何從 javascript 對像中獲取名稱和值到新列表中? (How to get name and values from a javascript object into a new list?)

數據未發布..幫助!html,js,firebase (Data not posting.. Help! html,js,firebase)

使用 Node.js 腳本查看表單數據 (Seeing form data with Node.js script)

使用百分比查找範圍內的值 (find the value within a range using percent)

如何通過 react.js 中的組件傳遞變量或數據? (How to pass varible or data through components in react.js?)







留言討論