【发布时间】:2019-03-08 18:41:44
【问题描述】:
我正在尝试使用 AWS 移动后端(使用 lambda 函数)插入 dynamoDB(也在移动后端配置),但到目前为止没有成功。
相关代码:
'use strict';
console.log("Loading function");
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region:process.env.MOBILE_HUB_PROJECT_REGION});
exports.handler = function(event, context, callback) {
var responseCode = 200;
var requestBody, pathParams, queryStringParams, headerParams, stage,
stageVariables, cognitoIdentityId, httpMethod, sourceIp, userAgent,
requestId, resourcePath;
console.log("request: " + JSON.stringify(event));
// Request Body
requestBody = event.body;
if (requestBody !== undefined && requestBody !== null) {
// Set 'test-status' field in the request to test sending a specific response status code (e.g., 503)
responseCode = JSON.parse(requestBody)['test-status'];
}
// Path Parameters
pathParams = event.path;
// Query String Parameters
queryStringParams = event.queryStringParameters;
// Header Parameters
headerParams = event.headers;
if (event.requestContext !== null && event.requestContext !== undefined) {
var requestContext = event.requestContext;
// API Gateway Stage
stage = requestContext.stage;
// Unique Request ID
requestId = requestContext.requestId;
// Resource Path
resourcePath = requestContext.resourcePath;
var identity = requestContext.identity;
// Amazon Cognito User Identity
cognitoIdentityId = identity.cognitoIdentityId;
// Source IP
sourceIp = identity.sourceIp;
// User-Agent
userAgent = identity.userAgent;
}
// API Gateway Stage Variables
stageVariables = event.stageVariables;
// HTTP Method (e.g., POST, GET, HEAD)
httpMethod = event.httpMethod;
// TODO: Put your application logic here...
let params = {
Item:{
"prop1":0,
"prop2":"text"
},
TableName:"testTable"
};
docClient.put(params, function(data, err){
if(err)
responseCode = 500;
else
{
responseCode = 200;
context.succeed(data);
}
});
// For demonstration purposes, we'll just echo these values back to the client
var responseBody = {
requestBody : requestBody,
pathParams : pathParams,
queryStringParams : queryStringParams,
headerParams : headerParams,
stage : stage,
stageVariables : stageVariables,
cognitoIdentityId : cognitoIdentityId,
httpMethod : httpMethod,
sourceIp : sourceIp,
userAgent : userAgent,
requestId : requestId,
resourcePath : resourcePath
};
var response = {
statusCode: responseCode,
headers: {
"x-custom-header" : "custom header value"
},
body: JSON.stringify(responseBody)
};
console.log("response: " + JSON.stringify(response))
context.succeed(response);
};
由于某种原因,这不会将项目放在桌子上。 我使用角色部分授予了必要的权限,还有什么遗漏吗?
**responseCode 仅用于测试目的。
编辑: 试过AWS node.js lambda request dynamodb but no response (no err, no return data) 也没用。
编辑2: 添加了完整的处理程序代码。 (它是创建第一个 AWS lambda 时默认生成的代码)。
【问题讨论】:
-
您能否发布您的整个 Lambda 处理程序?我有一些想法,但需要先检查您的处理程序。
-
谢谢,我添加了完整的代码(从 AWS 自动生成的)。我应该提一下,这只是一个测试函数,用于学习如何使用 lambda 访问数据库。 (做大学项目,想用云数据库)
-
请同时添加您在执行此代码时收到的确切错误消息。
-
根本没有错误消息.. 它只是没有将项目添加到数据库中。
-
我也在考虑这个问题。谢谢你的建议。我会联系文档团队
标签: amazon-web-services amazon-dynamodb