gpt4 book ai didi

javascript - 为什么我的 Lambda 函数在完成请求之前就退出了?

转载 作者:行者123 更新时间:2023-12-03 04:35:21 25 4
gpt4 key购买 nike

我有以下 Lambda 函数,它是我按照 PluralSight 教程编写的[我对 js 知之甚少。这样做是为了学习 API Gateway <--> Lambda 教程]

我编写了完全相同的函数,只是使用 {} 作为测试 json:

var faker = require('faker');

exports.handler = function(event, context){
var inventory = [];
for(var i = 0; i < 10; i++){
var shoe = {};
var shoeType = getShoeType();
shoe.name = getShoeName(shoeType);
shoe.color = getShoeColor();
shoe.description = getShoeDescription(showType);
shoe.size = getShoeSize();
shoe.price = getShoePrice();

inventory.push(shoe);
}

context.succeed(inventory);
}

function getShoeName(showType){
return faker.company.catchPhraseNoun() + " " + faker.company.catchPhraseDescriptor() +
" " + showType;
}

function getShoeColor(){
return faker.commerce.color();
}

function getShoeDescription(shoeType){
return "A(n)" + faker.commerce.productAdjective() + ", " + faker.commerce.productAdjective() +
" " + shoeType + "made from the finest " + faker.commerce.productMaterial() + "designed for the " +
faker.commerce.bsBuzz() + " individual!";
}

function getShoeSize(){
return getNum(1, 13);
}

function getShoePrice(){
return faker.commerce.price();
}


function getShoeType(){
var shoeType = [
"running shoes",
"training shoes",
"tennis shoe",
"cricket shoe",
"other shoe"
]
return shoeType[getNum(0, 5)];
}

function getNum(min, max){
return Math.floor(Math.random() * (max - min +1)) + min;
}

但是,Lambda 给出了此错误:

{
"errorMessage": "RequestId: e5566a3c-1df8-11e7-8b71-d961323b4fcf Process exited before completing request"
}

我也检查了 context.succeed() 的位置,完全没问题。

那么,我哪里出错了?

最佳答案

我在您的 getShoeType() 函数中看到一个错误。该函数有一个包含 5 种鞋子类型的数组(数组索引从 0 到 4)。

但是由于您传递给随机数函数的参数,它返回从 0 到 5 的值。因此,当随机数函数返回 5 时,它会生成错误 b/c 显示类型数组的最后一个元素是位于索引 4,而不是 5。

编辑:经过进一步审查,这可能不会导致函数在完成请求之前退出...但您可能会在 getShoeName( 返回的字符串中看到“undefined”) )

编辑#2另一个错误是您将 undefined variable showType(而不是 shoeType)传递给 getShoeDescription() 函数。

在使用 AWS Lambda 时,我发现最令人沮丧的事情之一是看不到发生的真正错误。我最终开始使用 Serverless 框架,它的好处是可以让您在本地运行 Lamdba 函数 - 如果您这样做,您将获得更好的错误消息。

关于javascript - 为什么我的 Lambda 函数在完成请求之前就退出了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43325784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com