Lost the data after dynamodb operation




Before telling about the problem, I would like to present about the thing I am doing. I need to access data from three dynamo table. For example.

  1. UserTable - user_id, zone_ids (in list)
  2. ZoneTable - zone_id, device_ids [in list)
  3. RegionTable - region_id, zone_ids (in list)

I want to retrieve all device in a zone that associate with a user.
So, firstly, I tried to get the list of zone_id from user table which is going to use when I try to get list of device from zone table by using zone_id getting from user table.

The problem is that when I tried to push the data (zone_ids) from dynamodb scan function(scanning the user table to get zone_ids) to a list outside of DB scan code scope, I get only empty list rather than list of data (zone_ids).

How can I add the data into variable (list) from inside of dynamodb scan method.

var tmp = [ ]
docClient.scan( params, (error, result) => {
if(error) … else{ tmp.push(…) }
console.log(tmp) //empty list

What should I do?

Many thanks,
Kind Regards,


Hi John.

I’d suggest you take a refresher on asynchronous programming, callbacks and promises in Node. The callback function in docClient.scan() is called once data is available but the call to docClient.scan() can return before then. This means that tmp in console.log(tmp) is virtually (but not 100%) guaranteed to return an empty list.

Using callbacks the only approach I know is to put your code that uses tmp inside the callback itself.

You could also look at using Promises that can be chained or async/await. If it was me I’d go with updating the runtime to node 8.10 then using async/await as it’s a lot less confusing.


Hello @buggy finally it is working with promises. Thanks mate.