我有以下 Lambda 函数,这是我按照 PluralSight 教程编写的 [我对 js 知之甚少。为学习API网关而做<--> 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()
.
编辑 #2另一个错误是您将未定义的变量showType
(而不是shoeType
)传递给getShoeDescription()
函数。
没有看到发生的真正错误是我在使用 AWS Lambda 时感到沮丧的事情之一。我最终开始使用无服务器框架,它的好处是让您可以在本地运行您的 Lamdba 函数——如果这样做,您将获得更好的错误消息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句