issue returning value from client.folders.getItems

Highlighted
New Contributor

issue returning value from client.folders.getItems

Hi, 

 

I'm new to javascript and I think i must have missed something simple here, any help would be greatly appreciated.

 

I'm using an IIFE to return a list of folder names. I'm able to access the names from the returned json nested inside "items" on the "element.name" property. Currently it works to console log the "value" variable inside the callback, however, when i try to access the returned "value" it is undefined. 

 

 

 

var BoxList = (function(token) {
    // Initialize the SDK with your app credentials
    var sdk = new BoxSDK({
    clientID: ClientID,
    clientSecret: ClientSecret
    });

    
    // Create a basic API client, which does not automatically refresh the access token
    var client = sdk.getBasicClient(token);

    client.users.get(client.CURRENT_USER_ID)

    return {
        publicProjectList: function() {
                    
                    var value;
                    client.folders.getItems(folderID, {
                        usemarker: 'false',
                        fields: 'name',
                        offset: 0,
                        limit: 50
                        })
                        .then(items => {items.entries.forEach(element => { 
                            value = element.name;
                            //console.log(value);
                            return value;
                            }) 
                        } ) 
                        .catch(err => console.log('Got an error!', err));
                        return value;
                    }
                }
       }
})(devToken, ClientID, ClientSecret, folderID);

console.log(BoxList.publicProjectList().value);

 

 

 

Thanks for taking a look

1 REPLY 1
Highlighted
New Contributor

Re: issue returning value from client.folders.getItems

just to close the loop for anyone that comes accross this, there is a conflict with the syncronus and asyncronus portions of code. the ".getItems" method needs to be awaited in order to return all the items, so the whole function needs to be wrapped in a a async function in order to properly wait and use the information. 

 

sorry for the new to javascript error.