3

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;
};

enter image description here

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?

3
  • Check again process.env.DYNAMODB_TABLE value. Commented Jan 21, 2020 at 10:47
  • @hoangdv Yeah it has a valid value Commented Jan 21, 2020 at 11:18
  • @preethiR ??? :D . I don't think so, I think you do not have any table with name is process.env.DYNAMODB_TABLE on your system. dynamoDb.get always return success if item with id value is job.Id existed or not. In not found case, you can check data.Item === null. Commented Jan 22, 2020 at 1:19

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

10 Comments

I have changed my code as you have suggested. But the control flow never goes inside if(!result) block. Instead it is going into catch block.
I figured it out. I have just moved the latestJobs.push(job) inside .catch block. And now it is working.
@YashwardhanPauranik glad it worked. by the way what is the error you are getting in catch?
Why can't you write clean and module function to handle this, the code above looks unhealthy
@YashwardhanPauranik This mean latestJobs.push(job); always be call, although item is existed or not ???
|
0

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.

6 Comments

The above code is modular, but when I split I have to run 2 loops instead of one.
Look at this way, within a loop every time you are trying to do some expensive operation and other jobs are waiting. Why not iterate over loop of params we need? Initial loop we are iterating will have no performance impact. Code looks clean and neat. More important is, it is human readable.
There will be no` job` instance in the catch block
If job is not found you get the error right, and param include the ` id: job.Id` right?
The variable job is not present in the getJobs() where it is being pushed. Hence wil throw error.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.