Tuesday 9 May 2017

access values after authentication in node js

I've a program that does the below.

  • Look into a DynamoDB table.
  • Get the data from the table.
  • Save the variables in session
  • After the process, print the values in console.

My code is as below.

intentHandlers['GetMYBusinessInfo'] = function (request, session, response, slots) {
    console.log('entered ext bloxk');

    if (!session.attributes.userName) {
        console.log('eneterd the user entered the block');
        var userName = 'jim';
        isUserRegistered(userName.toLowerCase(), function (res, err) {
            if (err) {
                response.fail(err);
                console.log(err);
            }
            else if (!res) {
                response.shouldEndSession = true;
            }
            else {
                console.log(res);
                var countRes = JSON.stringify(res.Count);
                var unpUserRegion = JSON.stringify(res.Items[0].Region);
                var unpUserCity = JSON.stringify(res.Items[0].State);
                var userRegion = JSON.parse(unpUserRegion);
                var userCity = JSON.parse(unpUserCity);
                session.attributes.city = userCity;
                session.attributes.region = userRegion;
                console.log("parsed   " + countRes + "\t region is " + userRegion);
                session.attributes.userName = true;
            }
        });
    }
    console.log(`session values after authentication are user city is ${session.attributes.city}`);
}

The method to check if the value is in DynamoDb or not.

function isUserRegistered(userName, callback) {
    var params = {
        TableName: "usersTable",
        FilterExpression: "#nme = :nme",
        ExpressionAttributeNames: {
            "#nme": "Name",
        },
        ExpressionAttributeValues: {
            ":nme": userName
        }
    };

    var count = 0;
    docClient.scan(params, function (err, data) {
        if (err) {
            console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2));
            callback(false, err);
        } else {
            console.log("Scan succeeded." + data.Items.length);
            if (data.Items.length === 0) {
                callback(false);
            }
            else {
                data.Items.forEach(function (itemData) {
                    console.log("Item :", ++count, JSON.stringify(itemData));
                });
                callback(data);
            }

        }
    });
}

when I run this, the output that I get is:

session values after authentication are user city is undefined

Scan succeeded.1

Item : 1
{
    "ID": "3",
    "State": "wisconsin",
    "Region": "midwest",
    "Name": "jim"
}

{ Items: [ { ID: '3', State: 'wisconsin', Region: 'midwest', Name: 'jim' } ],
Count: 1,
ScannedCount: 1 }

parsed 1    region is midwest

Here I know that Node js being Non-blockable process, the above output is correct, but I want to get the value of city printed in session values after authentication are user city is {HereTheCityComes} instead of session values after authentication are user city is undefined.

I'm sure that placing the console.log(session values after authentication are user city is ${session.attributes.city}); in the last else block(place where the data is returned).

But I need this type of functionality(Get data as shown in my current scenario), as there is some other things to be done after checking if the user is available in database.

please let me know where am I going wrong and how can I fix this.



via user3872094

No comments:

Post a Comment