# Lost the data after dynamodb operation

**URL:** <https://forum.serverless.com/t/lost-the-data-after-dynamodb-operation/4169>\
**Category:** Serverless Framework\
**Tags:** lambda\
**Created:** [April 5, 2018, 5:25am UTC](https://forum.serverless.com/t/lost-the-data-after-dynamodb-operation/4169 "2018-04-05T05:25:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnWai](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@JohnWai](https://forum.serverless.com/u/JohnWai)\
**Post date:** [April 5, 2018, 5:25am UTC](https://forum.serverless.com/t/lost-the-data-after-dynamodb-operation/4169/1 "2018-04-05T05:25:47Z")

</div>

Hi,

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,  
John

---

<div class="post-metadata">

**Author:** ![buggy](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/buggy/32/177_2.png) [@buggy](https://forum.serverless.com/u/buggy)\
**Post date:** [April 9, 2018, 11:55am UTC](https://forum.serverless.com/t/lost-the-data-after-dynamodb-operation/4169/2 "2018-04-09T11:55:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JohnWai](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@JohnWai](https://forum.serverless.com/u/JohnWai)\
**Post date:** [April 12, 2018, 11:22pm UTC](https://forum.serverless.com/t/lost-the-data-after-dynamodb-operation/4169/3 "2018-04-12T23:22:23Z")

</div>

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