cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Are you interested in learning how media industry leaders use Dropbox Replay? Register for our webinar here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

i want to check is file exists with async/await

i want to check is file exists with async/await

brtdvrs
Explorer | Level 4

I tried this but it doesn't work:

async function CheckIfFileExist(dbx, fileName) {
let fentries = [];
dbx.filesListFolder({ path: "" })
.then(async function (response) {
fentries = await response.result.entries;
if (fentries > 1) {
for (let i = 0; i < fentries.length; i++) {
if (fentries[i].name == fileName) {
return true
}
return false
}
} else {
return false
}
})
.catch(function (error) {
alert(error);
});
}

3 Replies 3

Greg-DB
Dropbox Staff

Please clarify what you mean when you say "it doesn't work". For example, what unexpected error or output do you get from the API?

 

From what you shared though, note that you need to implement filesListFolderContinue in addition to filesListFolder as documented to make sure you get all entries. You're not guaranteed to get everything back from one call to filesListFolder.

 

Also, as written, this will only start listing the direct children of the root folder. If you're looking for a file elsewhere, this code won't find it. For example, you would need to set FilesListFolderArg.recursive to true to get nested entries. Also, in case you need to check a team space, check out the Team Files Guide.

 

In any case, if you know the actual path to the potential file, you can just call filesGetMetadata.

brtdvrs
Explorer | Level 4
I just have to look in de root for the file. That’s not the problem. My call comes back undefined and not true or false because the await response does not work.

Greg-DB
Dropbox Staff

Thanks for clarifying. It sounds like the issue is more around how async/await itself works in JavaScript. I can only really help with the Dropbox API/SDK in particular, so I recommend referring to a guide/documentation for async/await in JavaScript.

 

That said, it seems like "await response.result.entries" may not be what you want. Perhaps you mean to await the API call itself?

 

Also, "fentries > 1" doesn't seem to make sense. I suspect you meant "fentries.length >= 1" instead.

Need more support?
Who's talking

Top contributors to this post

  • User avatar
    Greg-DB Dropbox Staff
  • User avatar
    brtdvrs Explorer | Level 4
What do Dropbox user levels mean?