简体   繁体   English

如果 dynamodb 中不存在项目,则无法返回数组

[英]Cannot return array if item not present in dynamodb

I have a function that will take an array of jobs as a parameter in it.我有一个函数,它将一系列jobs作为其中的参数。 This function will check the existence of each job in the database through its id .此函数将通过其id检查数据库中每个job的存在。

If a job is not to present in the database, that particular job needs to be pushed into an array called latestJobs .如果某个作业不存在于数据库中,则需要将该特定作业推送到名为latestJobs的数组中。 I'm calling this function in my main.js file.我在main.js文件中调用这个函数。 But the code breaks and stops.但是代码中断并停止。

Below is my main.js code:下面是我的main.js代码:

module.exports.app = async () => {
 try {
    ...
    const jobs = await getJobsForCountries(body);
    const latestJobs = await filterPreDraftedJobs(jobs);
    console.log('latestJobs', latestJobs);

  } catch (e) {
    console.error('Error:- ', e); // Comes to here
  }
};

My checker function looks like:我的检查器功能如下所示:

module.exports = async (jobs) => {
  let latestJobs = [];
  for (const job of jobs) {
    const params = {
      TableName: process.env.DYNAMODB_TABLE,
      Key: {
        id: job.Id
      }
    };
    await dynamoDb.get(params, (err, data) => {
      if (err) {
        latestJobs.push(job);
        console.log('Job not found in DB'); 
      }
    }).promise();
  }
  return latestJobs;
};

在此处输入图片说明

How can I fix this issue?我该如何解决这个问题? I want the latestJobs which will not present in the database.我想要不会出现在数据库中的latestJobs Is there a function for dynamodb which can do this for me? dynamodb 是否有可以为我执行此操作的功能?

You are mixing callback, promise and await style.您正在混合回调、承诺和等待风格。 I would do it like this我会这样做

module.exports = async (jobs) => {
  let latestJobs = [];
  for (const job of jobs) {
    const params = {
      TableName: process.env.DYNAMODB_TABLE,
      Key: {
        id: job.Id
      }
    };
    try {
      const result = await dynamoDb.get(params).promise();
      if (result) {
       return; 
      }
    } catch(err) {
      latestJobs.push(job);
    }
  }
  return latestJobs;
};

Also, make sure that table is created and the region and name you are passing is correct.此外,请确保创建了表并且您传递的区域和名称正确无误。

I am not much familiar with dynamoDB but looking at the above conversation code must be something like this.我对dynamoDB不是很熟悉,但是看上面的对话代码一定是这样的。 I have tried to improve performance and making sure the code is modular and readable.我试图提高性能并确保代码是模块化的和可读的。

async function addUpdateJobs(jobs)
{
    let paramsArray = [];
    for (const job of jobs)
    {
        const jobParams = {
        params:{
            TableName: process.env.DYNAMODB_TABLE,
            Key: {
                id: job.Id
            }               
        },
         job:job
    };
        paramsArray.push(jobParams );

    }
    return await this.getJobs(paramsArray);
}


function getJobs(paramsArray)
{
    let latestJobs = [];
    paramsArray.each(async (jobParam)=>
    {
        try
        {
            const result = await dynamoDb.get(jobParam.params).promise();
            if (result)
            {
                return;
            }
        } catch (err)
        {
            latestJobs.push(jobParam.job);
        }
    });
    return latestJobs;

}

PS: Also I was gonig through error handling in amazondynamodb . PS:另外我是通过错误处理gonig在amazondynamodb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM