简体   繁体   中英

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. This function will check the existence of each job in the database through its id .

If a job is not to present in the database, that particular job needs to be pushed into an array called latestJobs . I'm calling this function in my main.js file. But the code breaks and stops.

Below is my main.js code:

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. Is there a function for dynamodb which can do this for me?

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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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