【发布时间】:2018-02-09 07:29:58
【问题描述】:
我有一个要求,我需要查询一个表,然后循环遍历结果并在循环内查询另一个表。但我尝试使用 synchronize 节点 js 模块。但有时 api 使用此方法以超时错误结束。现在我正在以下面的方式尝试我的代码,但没有运气。
var notification = {
TableName: "Tablename",
KeyConditionExpression:"#userId =:userid and #datetime <=:datetime",
ScanIndexForward: false,
ExpressionAttributeNames: {
"#userId": "userId",
"#datetime":"datetime"
},
ExpressionAttributeValues: {
":userid" :userId,
":datetime" :datetime
}
};
docClient.query(notification, function(err, result) {
if (err) {
context.succeed({success: false, message : JSON.stringify(err, null, 2),method:"notification",type:type});
}else{
result = result.Items;
if (result.length>0) {
for(var i=0;i<result.length;i++){
result[k]['CafeDetail']=getDetail(result[k].CafeID,function (data) {
});
if (k ==(result.length -1)) {
context.succeed({success: true,data:result, message : "Notification list",method:"notification",type:type});
}
}
}
}
});
function getDetail(CafeID,callback) {
var param = {
TableName: "Tablename",
ProjectionExpression:"CafeName,Cafe_MainImage",
KeyConditionExpression:"CafeID =:cafeID",
ExpressionAttributeValues: {
":cafeID" :CafeID
}
};
docClient.query(param, function(err, CafeDetail) {
if (err) {
console.log (err)
callback(err);
} else {
callback(CafeDetail.Items);
console.log(CafeDetail.Items);
}
});
}
在输出时,这个变量 (result[k]['CafeDetail']) 总是以 undefined 的形式出现。但是 getDetail 函数中的 console.log 会打印结果。
我是使用 node js 的 aws lambda 新手。任何帮助将不胜感激。
提前致谢
【问题讨论】:
标签: node.js asynchronous lambda synchronization amazon-dynamodb