Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如果项目没有';t出口_Node.js_Aws Lambda_Amazon Dynamodb_Amazon Rekognition - Fatal编程技术网

Node.js 如果项目没有';t出口

Node.js 如果项目没有';t出口,node.js,aws-lambda,amazon-dynamodb,amazon-rekognition,Node.js,Aws Lambda,Amazon Dynamodb,Amazon Rekognition,我正在用aws rekognition设置一个流媒体视频的出勤检查,当一个人被确认时,lambda应该将其写入DynamoDB。insertDynamo()单独工作很好(当我不将其作为函数调用时),但当我将其放入函数中时,它不会写入DynamoDB表。知道我做错了什么吗 var AWS=require('AWS-sdk'); var today=新日期(); var date=today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDa

我正在用aws rekognition设置一个流媒体视频的出勤检查,当一个人被确认时,lambda应该将其写入DynamoDB。
insertDynamo()
单独工作很好(当我不将其作为函数调用时),但当我将其放入函数中时,它不会写入DynamoDB表。知道我做错了什么吗

var AWS=require('AWS-sdk');
var today=新日期();
var date=today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var hour=today.getHours()+“:“+today.getMinutes()+”:“+today.getSeconds();
exports.handler=异步(事件、上下文)=>{
//log('Received event:',JSON.stringify(event,null,2));
event.Records.forEach((记录)=>{
//动觉数据是base64编码的,所以在这里解码
const load=新缓冲区(record.kinisis.data,'base64')。toString('ascii');
const payload=JSON.parse(load);
if(payload.FaceSearchResponse!=null)
{
payload.FaceSearchResponse.forEach((面)=>{
if(face.MatchedFaces!=null&&
Object.keys(face.MatchedFaces).length>0
{
var id=JSON.stringify(face.MatchedFaces[0].face.ExternalImageId,null,4);
//这是硬代码,它需要将字符串与动画(id)分开
insertDynamo(日期,小时,'0001');
}
其他的
{
//无所事事
}
});
}
});
返回`已成功处理${event.Records.length}条记录。`;
};
var insertDynamo=函数(日期、小时、id){
exports.handler=异步(事件、上下文)=>{
const documentClient=new AWS.DynamoDB.documentClient();
让responseBody=“”;
设statusCode=0;
常量参数={
TableName:“用户”,
项目:{
证件号码:id,,
协助:{
日期:日期:,
小时:小时
}
},
ConditionExpression:'属性不存在(徽章编号)'
};
试一试{
const data=await documentClient.put(params.promise();
responseBody=JSON.stringify(数据);
状态代码=201;
}捕捉(错误){
responseBody=`无法放置产品:${err}`;
statusCode=403;
}    
常数响应={
状态代码:状态代码,
标题:{
“内容类型”:“应用程序/json”
},
主体:响应体
}
返回响应
}
};

您的lambda函数在被调用时将立即完成,因为您使用
.forEach
循环遍历所有记录。这意味着您所有插入DynamoDB的请求都将在他们完成YOB之前被取消

对于您的案例,我有两种解决方案:

  • 等待lambda回调堆栈清除

    只需在lambda函数前加一行“config”

       context.callbackWaitsForEmptyEventLoop = true;
    
  • 使用旧式for loop代替
    forEach
    (推荐)
    forEach
    使用回调样式来解决每个项目,那么它将无法像我们预期的那样使用
    async/await
    关键字


  • 是否抛出错误?迪纳摩有什么回应吗?
    var AWS = require('aws-sdk');
    var today = new Date();
    var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
    var hour = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
    
    exports.handler = async (event, context) => {
      //console.log('Received event:', JSON.stringify(event, null, 2));
    
      for (const record of event.Records) { // for of instead of forEach
        const load = new Buffer(record.kinesis.data, 'base64').toString('ascii');
        const payload = JSON.parse(load);
        if (payload.FaceSearchResponse != null) {
          for (const face of payload.FaceSearchResponse) { // for of instead of forEach
            if (face.MatchedFaces != null &&
              Object.keys(face.MatchedFaces).length > 0) {
              var id = JSON.stringify(face.MatchedFaces[0].Face.ExternalImageId, null, 4);
              //this is hard code it ---needs to split string from kinesis(id)
              await insertDynamo(date, hour, '0001'); // wait until task finish then solve next item
            }
            else {
              //do nothing
            }
          }
        }
      }
      return `Successfully processed ${event.Records.length} records.`;
    };
    
    var insertDynamo = function (date, hour, id) {
      // What is this?????????
      // exports.handler = async (event, context) => {
    
      // }
    
      const documentClient = new AWS.DynamoDB.DocumentClient();
    
      const params = {
        TableName: "users",
        Item: {
          badgeNumber: id,
          assistance: {
            date: date,
            hour: hour
          }
        },
        ConditionExpression: 'attribute_not_exists(badgenumber)'
      };
    
      return documentClient.put(params).promise(); // enough for us
    };