-1

I'm trying to get the path of file or folder by its ID.

I know that parents are just like tags but with Google Drive on the pc it's not the case: they are always folder and file in its hierarchical structure.

So I try this code:

function getPath( fileId ) {

    path = ""
    names = [];

    while ( id != '0AN9ZF6qdu2FUUk9PVA' ){ //the right string is the root id

        get_metadata( id )
            .then( function(response){
                    if( response.result.parents )
                        names.push( response.result.name )

                    id = response.result.parents[0];
            })
    }

    path = names.reverse.join('/');

    return path;
}

It doesn't work. Help me please.

1 Answers1

0

The most likely reason for it not working is that you have failed to deal with the asynchronous nature of JavaScript. Your statements will execute something like:-

get_metadata
get_metadata
get_metadata
get_metadata
get_metadata
get_metadata
get_metadata
get_metadata
names.push
get_metadata
get_metadata
names.push

Secondly, your function param is fileId but this is never used in the function body. Instead you are using id.

It's worth looking at option 3 of How do I search sub-folders and sub-sub-folders in Google Drive? which means you can do what you are trying to do in a single GDrive API call.

So, with the best will in the world, you have a lot of JavaScript learning you need to do.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • I always used Promise in efficient way (i.e. Dropbox API), but the new deal here is about convert a file id to a full path. Nevermind the variables id and fileId, I mismatch that variables just when I wrote the comment. I think if your link is valid I can resolve my issue in two hours or less. Thanks. – Pierpaolo Fernandez Jul 26 '18 at 17:38
  • 1
    No, your link doesn't help me. Probably you didn't understand my question. I get the id ( AZ45wefds2561651113213 ) of a file and I want retrieve the full path : "Public/Photos/AnotherFolderName/theFIleWithThatID". – Pierpaolo Fernandez Jul 26 '18 at 18:47
  • I understand what you are trying to do. Unfortunately I think you will fail. – pinoyyid Jul 26 '18 at 21:23
  • 1
    @PierpaoloFernandez it will be much simpler for you if you can reduce the number of async calls. The linked option 3 means you use 1 request to get folders & their parent folders, and 1 request to figure out which folder is the parent of the file you're searching. then voila, you have everything in memory and your promise resolution code can ignore nesting promises. – tehhowch Jul 27 '18 at 05:02
  • I resolved by me, first with the recursive function, then with the option 3 that actually is the best solution. Thanks. – Pierpaolo Fernandez Jul 27 '18 at 09:49