1

I have a function below that grabs data from DynamoDB and then returns true or false upon evaluation of a condition. This function will be used to do a simple check and determine the elevation of the user for data access and whatnot.

How can I get this function to do something like:

var auth = aliasHasRole('foo','bar')
console.log(auth) // prints value passed down by aliasHasRole().

I figured I had to declare it as async and added await before returning but no luck, then did aliashHasRole('foo','bar').then( (x) => { auth = x}), but it returns undefined.


Here's the full code:

var AWS = require('aws-sdk');
AWS.config.update({
    region: 'us-west-2',
    accessKeyId: "xxx",
    secretAccessKey: "xxx",
});
const docClient = new AWS.DynamoDB.DocumentClient();


function aliasHasRole(an_alias, a_role) {
    const params = {
        TableName: 'xxx',
        KeyConditionExpression: '#alias= :alias AND #Role= :Role',
        ExpressionAttributeNames: {
            '#alias': 'alias',
            '#Role': 'Role'
        },
        ExpressionAttributeValues: {
            ':alias': an_alias,
            ':Role': a_role,
        }
    };

    docClient.query(params).promise()
        .then(
            (data) => {
                //this line below returns true or false, how can I get I pass this value so I can return it from the aliasHasRole as true or false?
                console.log(data.Items.length > 0 && data.Items[0].alias === an_alias && data.Items[0].Role === a_role ? true : false);
                return data.Items.length > 0 && data.Items[0].alias === an_alias && data.Items[0].Role === a_role ? true : false;
            })
        .catch((err) => {
            console.log(err)
        })
};

var auth;
aliasHasRole("xxx","TeamManager");//should return true or false just like it logs it to the console.
//Do something to assign functions value to var auth.
console.log(auth) //print value passed by function...

//How can I assign this value to a variable? as in var auth = aliasHasTole('foo','bar') // auth is now true or false.
2
  • Your Node version is greator then 8 right ? Commented Feb 5, 2020 at 16:15
  • Sorry I said 8.1 but it seems I'm using Node.js 10 at least, see Commented Feb 5, 2020 at 16:21

1 Answer 1

2

You are not using the async/await keyword right.Modify your function like this and try.

var AWS = require('aws-sdk');
AWS.config.update({
    region: 'us-west-2',
    accessKeyId: "xxx",
    secretAccessKey: "xxx",
});
const docClient = new AWS.DynamoDB.DocumentClient();

// you can use async and await like this
let aliasHasRole = async function (an_alias, a_role) {

    try {
        const params = {
            TableName: 'xxx',
            KeyConditionExpression: '#alias= :alias AND #Role= :Role',
            ExpressionAttributeNames: {
                '#alias': 'alias',
                '#Role': 'Role'
            },
            ExpressionAttributeValues: {
                ':alias': an_alias,
                ':Role': a_role,
            }
        };

        // this will resolve the value 
        let data = await docClient.query(params).promise()
        return data.Items.length > 0 && data.Items[0].alias === an_alias && data.Items[0].Role === a_role ? true : false;
    }
    catch (err) {
        //this is equivalent .catch statement
        console.log(err)
    }
};
// This has to be self executing function in case of async await
(async () => {
    var auth = await aliasHasRole("xxx", "TeamManager");
    // This will print the value of auth which will be passed from the aliasHasRole ie. True or False
    console.log(auth) //print value passed by function aliasHasRole...
})()

You can also use this without async/await


var AWS = require('aws-sdk');
AWS.config.update({
    region: 'us-west-2',
    accessKeyId: "xxx",
    secretAccessKey: "xxx",
});
const docClient = new AWS.DynamoDB.DocumentClient();

// you can use async and await like this
function aliasHasRole(an_alias, a_role) {

    const params = {
        TableName: 'xxx',
        KeyConditionExpression: '#alias= :alias AND #Role= :Role',
        ExpressionAttributeNames: {
            '#alias': 'alias',
            '#Role': 'Role'
        },
        ExpressionAttributeValues: {
            ':alias': an_alias,
            ':Role': a_role,
        }
    };

    // Return is the main part. The error was that you are not using the return key word with Promise that's why it was not working
    return docClient
        .query(params)
        .promise()
        .then(data => data.Items.length > 0 && data.Items[0].alias === an_alias && data.Items[0].Role === a_role ? true : false)
        .catch(error => {
            // You can handle the error here
            console.log(error)
        })
};


aliasHasRole("xxx", "TeamManager").then(auth => {
    // This will print the value of auth which will be passed from the aliasHasRole ie. True or False
    //print value passed by function...
    console.log(auth)
})

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

5 Comments

It says var auth = await aliasHasRole("xxx", "SuperAdmin"); SyntaxError: await is only valid in async function
Please check again. I have edited the answer and also added a secondary way to achieve thing. In case of async/await you have to use async under self executing function inorder to avoid this syntax error
Hey both approaches work now but second approach seems more clean-ish? What's a better practice?
Ironically the first approach is widely and more clean way to write the js code. It hides the beneath asynchronous code and its complexity. The first approach transpiles into the second approach and then get executed. This is build natively under nodejs from version greater than 8.0.
Yeah now that you mention it I think that the part that threw me of is the last part about the anonymous arrow function (Is it called like that?) but I can see how it would "make more sense" in my actual code. Thank you so much I will definitely go through this code and try to learn it all!

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.