0

Can someone give me a hand? I tried to figure it out but i ran out of ideas.

-------------dynamo.js....

module.exports.readUser = function (user_id) {
    AWS.config = new AWS.Config();
    AWS.config.update({region:  "eu-west-1"});

    var docClient = new AWS.DynamoDB.DocumentClient();
    var table = "user";
    var user_id = user_id;
    var params = {
        TableName: table,
        Key:{
            "user_id": user_id
        }
    };
    docClient.get(params, function(err, data) {
        if (err) {
            return err;
        } else {
            //console.log(data); <-- data is filled 
            return data;
        }
    });

var dynamo = require("./dynamo.js");
console.log(dynamo.readUser(4711998));
pushkin
  • 9,575
  • 15
  • 51
  • 95

2 Answers2

0

The data you log out inside the function is only the returned value for the anonymous function given to docClient.get as the second argument. The exported readUser function has no return statement and therefore it is undefined.

Because of the async nature of javascript you have to access the result via a callback like so:

module.exports.readUser = function (user_id, cb) {
    ...
    docClient.get(params, function(err, data) {
        if (err) {
            return err;
        } else {
            return cb(data);
        }
    });
}

var dynamo = require("./dynamo.js");
dynamo.readUser(4711998, function (user) {
    console.log(user);
});
giggo1604
  • 481
  • 1
  • 5
  • 14
0

docClient.get is an asynchronous function. You need promisify this function or use callback.

Use callback:

module.exports.readUser = function (user_id, cb) {
    AWS.config = new AWS.Config();
    AWS.config.update({ region: "eu-west-1" });

    var docClient = new AWS.DynamoDB.DocumentClient();
    var table = "user";
    var user_id = user_id;
    var params = {
        TableName: table,
        Key: {
            "user_id": user_id
        }
    };
    docClient.get(params, cb);
}

var dynamo = require("./dynamo.js");

dynamo.readUser(4711998, function (err, user) {
    if(err)
        console.error(err);
    else
        console.log(user);
});

Use promise:

module.exports.readUser = function (user_id) {
    AWS.config = new AWS.Config();
    AWS.config.update({ region: "eu-west-1" });

    var docClient = new AWS.DynamoDB.DocumentClient();
    var table = "user";
    var user_id = user_id;
    var params = {
        TableName: table,
        Key: {
            "user_id": user_id
        }
    };
    return new Promise(function(resolve, reject) {
        docClient.get(params, function(err, data) {
            if (err) {
                reject(err);
            } else {
                resolve(data);
            }
        });
    });
}

var dynamo = require("./dynamo.js");

dynamo.readUser(4711998).then(function (user) {
    console.log(user);
}).catch(function(err) {
    console.error(err);
});

Read more:

How do I promisify the AWS JavaScript SDK?

https://aws.amazon.com/ru/blogs/developer/support-for-promises-in-the-sdk/

Lemix
  • 2,415
  • 1
  • 15
  • 16