0

It's my first posting here so sorry if I'm not giving enough information about my problem. I'm not that familiar with coding but I'm trying to get this function to return the url of files in my dropbox account:

var imgUrl = function(path) {
    dropbox({
        resource: 'sharing/list_shared_links',
        parameters: {
            'path': '/' + path
        }
    }, (err, result, response) => {
        if (err) {
            return console.log(err);
        }
        res = result.links[0].url;
        res = res.replace('dl=0', 'dl=1');
        return res;
    });
}

console.log(imgUrl("img/tilesets/Inside_A1.png")); //undefined

But console.log(imgUrl("img/tilesets/Inside_A1.png")); for example, shows undefined. I does give the url when I change return res to return console.log(res) but I can't make it give the result without console.log. Can someone please tell me how to return the url when imgUrl(path) is used?

  • You should learn how callbacks works. Code that you written is asynchronous. Read about it [here](https://codeburst.io/javascript-what-the-heck-is-a-callback-aba4da2deced?gi=6cb5c33a78c9) – Rafał Figura Aug 22 '20 at 15:23
  • I assume the dropbox call to the file is asynchronous, so when you execute your imgUrl method it wont wait for the result but it returns undefined which is the default return value of functions in JavaScript. – Daniel Kemeny Aug 22 '20 at 15:26

0 Answers0