Node.js 将pathParameters正确发送到AWS Lambda NodeJS函数-从AWS控制台调用可以工作,但不能从浏览器/邮递员调用
我有一个NodeJS lambda函数,当从AWS控制台测试时可以工作,但当从浏览器或邮递员返回时,Node.js 将pathParameters正确发送到AWS Lambda NodeJS函数-从AWS控制台调用可以工作,但不能从浏览器/邮递员调用,node.js,amazon-web-services,aws-lambda,Node.js,Amazon Web Services,Aws Lambda,我有一个NodeJS lambda函数,当从AWS控制台测试时可以工作,但当从浏览器或邮递员返回时,“无法读取未定义的属性“id” 这是我在AWS控制台测试功能时用作测试事件的值: { "pathParameters": { "id": "gamuda" } } 我添加了console.log以查看上建议的“event”值,并注意从浏览器/邮递员调用时没有读取该值 2020-06-22T12:17:24.096Z 37
“无法读取未定义的属性“id”
这是我在AWS控制台测试功能时用作测试事件的值:
{
"pathParameters": {
"id": "gamuda"
}
}
我添加了console.log以查看上建议的“event”值,并注意从浏览器/邮递员调用时没有读取该值
2020-06-22T12:17:24.096Z 37f76d74-ef02-401e-89a6-c00eedcf56b3 INFO {}
如何正确读取该值?这是我的代码:const id=event.pathParameters.id代码>
module.exports.exportContactsByClientID = (event, context, callback) => {
console.log(event);
const id = event.pathParameters.id;
// const id = 'mkh';
const params = {
TableName: contactsTable,
IndexName: "client_code-created_at_local-index",
KeyConditionExpression: "client_code = :v_ID",
ExpressionAttributeValues: {
":v_ID": id,
},
ProjectionExpression: "client_code, created_at, contact_name, age, phone, email, dsr_ratio, max_eligibility, selected_banks"
};
return db.query(params)
.promise()
.then((res) => {
// console.log(res);
const items = res.Items;
// Headers + Columns
worksheet.cell(1, 1).string('Client').style({font: {bold: true}});
worksheet.cell(1, 2).string('Name').style({font: {bold: true}});
worksheet.cell(1, 3).string('Age').style({font: {bold: true}});
worksheet.cell(1, 4).string('Email').style({font: {bold: true}});
worksheet.cell(1, 5).string('Phone').style({font: {bold: true}});
worksheet.cell(1, 6).string('DSR Ratio').style({font: {bold: true}});
worksheet.cell(1, 7).string('Max Eligibility').style({font: {bold: true}});
// Rows
// items.sort((a, b) => (a.date > b.date) ? -1 : 1);
items.forEach((item, i) => {
console.log(item.contact_name);
worksheet.cell(i + 2, 1).string(item.client_code);
worksheet.cell(i + 2, 2).string(item.contact_name);
worksheet.cell(i + 2, 3).string(item.age);
worksheet.cell(i + 2, 4).string(item.email);
worksheet.cell(i + 2, 5).string(item.phone);
worksheet.cell(i + 2, 6).string(item.dsr_ratio);
worksheet.cell(i + 2, 7).string(item.max_eligibility);
});
console.log(contactsBucket);
console.log(id);
return workbook.writeToBuffer().then(buffer => {
console.log('inside writetoubgger')
var params = {
Bucket: contactsBucket,
Key: `contacts/${id}.xlsx`,
Body: buffer,
ACL: 'public-read'
}
// S3.upload(params, function(err, data) {
// if (err) {
// console.log(err, err.stack);
// } else {
// // callback(null, response(200, res));
// callback(null, response(200, res));
// }
// });
return S3.upload(params).promise().then(data => {
callback(null, response(200, res));
});
})
})
.catch((err) => callback(null, response(err.statusCode, err)));
};
当通过id获取联系人时,我在读取id时使用类似的方法,此功能工作正常:
module.exports.getContactsByClientID = (event, context, callback) => {
const id = event.pathParameters.id.toLowerCase();
const newDt = new Date();
const filterDt = new Date(newDt.getTime() - (newDt.getTimezoneOffset() * 60000 )).toISOString().split("T")[0];
var params = {
TableName: contactsTable,
IndexName: "client_code-created_at_local-index",
KeyConditionExpression: "client_code = :v_ID AND created_at_local = :v_created",
ExpressionAttributeValues: {
":v_ID": id,
":v_created": filterDt
},
ProjectionExpression: "client_code, contact_name, age, phone, email, dsr_ratio, max_eligibility, selected_banks"
};
return db.query(params)
.promise()
.then((res) => {
console.log(res);
callback(null, response(200, res));
})
.catch((err) => callback(null, response(err.statusCode, err)));
};
serverless.yml:
service: mhub-dsr-calculator-api
custom:
allowed-headers:
- Content-Type
- Authorization
- X-Api-Token
- X-Origin
- X-Amz-Date
- X-Amz-Security-Token
settings:
CONTACTS_TABLE: dsrcalculator-contacts-dev
CONTACTS_BUCKET: mhub-dsrcalculator-contacts
provider:
name: aws
runtime: nodejs12.x
environment: ${self:custom.settings}
region: ap-southeast-1
iamRoleStatements:
- Effect: "Allow"
Action:
- dynamodb:DescribeTable
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
- dynamodb:Query
Resource:
- "arn:aws:dynamodb:${self:provider.region}:*:table/${self:custom.settings.CONTACTS_TABLE}"
- Effect: "Allow"
Action:
- dynamodb:Query
Resource:
- "arn:aws:dynamodb:${self:provider.region}:*:table/${self:custom.settings.CONTACTS_TABLE}/index/client_code-created_at_local-index"
- Effect: "Allow"
Action:
- "s3:*"
Resource:
- "arn:aws:s3:::${self:custom.settings.CONTACTS_BUCKET}/*"
functions:
createPost:
handler: handler.createContact
events:
- http:
path: /contact
method: post
cors: true
updatePost:
handler: handler.updateContact
events:
- http:
path: /contact/{id}
method: put
cors: true
getAllPosts:
handler: handler.getAllContacts
events:
- http:
path: /contacts
method: get
cors: true
getContactsByClientID:
handler: handler.getContactsByClientID
events:
- http:
path: /contacts/{id}
method: get
cors: true
exportContactsByClientID:
handler: handler.exportContactsByClientID
events:
- http:
path: /export/{id}
method: get
cors: true
resources:
Resources:
PostsTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: "id"
AttributeType: "S"
- AttributeName: "client_code"
AttributeType: "S"
- AttributeName: "created_at_local"
AttributeType: "S"
KeySchema:
- AttributeName: "id"
KeyType: "HASH"
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:custom.settings.CONTACTS_TABLE}
我已经做了一整天了,可能错过了一些愚蠢的事情:(任何提示和指导都将不胜感激。我的问题与API网关上的设置有关。以前export/{id}
没有选中“使用Lambda代理集成”选项(因为我正在测试一些东西)
正如指南所说,这需要进行检查,以便我们的请求详细信息可以在活动中获得
还有一个问题-为什么我在AWS控制台上的测试没有失败
我的问题与API网关上的设置有关。以前export/{id}
未选中“使用Lambda代理集成”选项(因为我正在测试某些东西)
正如指南所说,这需要进行检查,以便我们的请求详细信息可以在活动中获得
还有一个问题-为什么我在AWS控制台上的测试没有失败