AWS node.js lambda请求dynamodb但无响应(无错误,无返回数据) [英] AWS node.js lambda request dynamodb but no response (no err, no return data)
问题描述
我正在使用react-native播放aws mobilehub,我希望它可以为我加快后端托管的速度。
I am playing the aws mobilehub with react-native and I was hoping that it can speed up the backend hosting for me.
但是,我无法使其后端API正常工作。经过他们的文档长期运行后,我确定了其lambda函数与dynamodb服务之间的问题。
However, I cannot get its backend API working. After a long run with their docs, I pin down the problem between its lambda function and dynamodb service.
任何想法都会受到赞赏!
Any thoughts are greatly appreciated!
正如标题所述:我的aws lambda函数可以请求其dynamodb,但没有响应。
这里出了什么问题?
或如何从AWS dynamodb获取调试信息? (我已启用并启用了Cloudtrial,但它似乎也没有dynamodb的操作日志。)
As the titled says: my aws lambda functions can request its dynamodb but has no response. What went wrong here? Or how can I get debug info from AWS dynamodb? (I gg and enabled Cloudtrial but it doesn't seem to have operation logs of the dynamodb too.)
这里有最简单的node.js 6.10代码:
Here I have the simplest node.js 6.10 codes:
const AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-2'});
const dynamodb = new AWS.DynamoDB.DocumentClient();
exports.handler = function(event, context, callback) {
var responseCode = 200;
var requestBody, httpMethod, res;
console.log("request: " + JSON.stringify(event));
// Request Body
requestBody = event.body;
/*testing dynamodb with put*/
console.log("PUT begins");
let putItemParams2 = {
TableName: "xxxx-mobilehub-xxxx-Test",//tableName
Item: {businessId:'putItemParams2r3',test:'yooo', hmm:'hhhh'}
};
console.log("putItemParams2: ",putItemParams2);
dynamodb.put(putItemParams2, (err, data) => {
console.log("putItemParams2");
if (err) console.log("dynamodb err: ",err, err.stack); // an error occurred
else console.log("dynamodb data: ",data); // successful response
});
console.log("PUT end");
var response = {
statusCode: responseCode
//....
};
...
//comment out context.succeed here to avoid program termination before intended.
//console.log("response: " + JSON.stringify(response))
//context.succeed(response);
};
日志
先前的代码为从AWS CloudWatch触发后,我可以看到日志:
Logs
When the previouse codes are triggered, from AWS CloudWatch I can see logs:
START RequestId: 3d7c5f7f-1b98-11e8-ad00-93a6d10c8f4e Version: $LATEST
[timestamp] PUT begins
[timestamp] putItemParams2: { TableName: 'xxx-mobilehub-xxxx-Test',
Item: { businessId: 'putItemParams2r3', test: 'yooo', hmm: 'hhhh'}}
[timestamp] put end
END RequestId: 3d7c5f7f-1b98-11e8-ad00-93a6d10c8f4e
所以没有错误,没有数据,没有响应。我检查了我的dynamodb,没有插入任何东西。
So no err, no data, no response. I checked my dynamodb and there is nothing insert.
条件#1:此动态表具有公共访问权限,因为我想排除身份验证问题。
condition#1: this dynamodb table has public access since I want to rule out the auth problem.
条件#2:我确保我的lambda函数可以访问这些表。例如arn:aws:dynamodb::xxxx:table / xxxx-mobilehub-xxxx-允许一切
condition#2: I ensure that my lambda function has access to these tables. e.g. arn:aws:dynamodb::xxxx:table/xxxx-mobilehub-xxxx- allow everything
条件#3:我为自己构建了一个简单的node.js执行(aws-sdk),并且该服务器使用相同的代码即可正常工作。
我能够获取&放入项目int&
condition#3: I build myself a simple node.js to execute the (aws-sdk)and this server works perfectly fine with the same code.. I am able to "get" &"put" items int & out from my dynamodb table.
我的本机代码使用'aws-amplify-反应本地。哪个API.put很好并且lambda函数至少正在接收api调用(来自问题#1)。
my react-native code use 'aws-amplify-react-native'. Which the API.put is fine and the lambda function is at least receiving the api call (from problem#1).
但是,API.get返回403错误,并且lambda函数甚至没有此操作的日志。
However, API.get returns me 403 error, and the lambda function doesn't even has log for this operation..
async function getBusiness(){
const path = "/Test";
const api = "TestCRUD";
let queryGetBusiness = {body: {userId: "hmmm"}};
try {
let apiResponse = await API.get(api, path, queryGetBusiness)//.then((data)=>{console.log(data)});
let apiResponseJson = await JSON.stringify(apiResponse);
console.log("response from saving Business: " + apiResponseJson);
}
catch (e) {console.log(e);}
}
PS(AWS可以用此mobilehub做得更好。他们的文档缺少详细信息,而我猜awsmobile cloud-api调用存在一些问题。)
P.S.(AWS could do much better with this mobilehub.. their documentation is lacking details and awsmobile cloud-api invoke has some problems I guess.)
推荐答案
const AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-2' });
const dynamodb = new AWS.DynamoDB.DocumentClient();
exports.handler = function (event, context, callback) {
var responseCode = 200;
var requestBody, httpMethod, res;
console.log("request: " + JSON.stringify(event));
// Request Body
requestBody = event.body;
/*testing dynamodb with put*/
console.log("PUT begins");
let putItemParams2 = {
TableName: "xxxx-mobilehub-xxxx-Test",//tableName
Item: { businessId: 'putItemParams2r3', test: 'yooo', hmm: 'hhhh' }
};
console.log("putItemParams2: ", putItemParams2);
dynamodb.put(putItemParams2, (err, data) => {
console.log("putItemParams2");
if (err) console.log("dynamodb err: ", err, err.stack); // an error occurred
else {
console.log("dynamodb data: ", data);
context.succeed(response);
}
// Call these here
console.log("PUT end");
});
//console.log("response: " + JSON.stringify(response))
};
确保在内部调用 context.succeed
回调函数。像上面一样。
Make sure you call context.succeed
inside the callback function. Like above.
您也可以只使用 handler
函数的第三个参数-回调
而不是 context.succeed 像
callback(null,response);
You can also just use the third argument to handler
function - callback
instead of context.succeed
like callback(null, response);
这篇关于AWS node.js lambda请求dynamodb但无响应(无错误,无返回数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!