Need to see if your shared folder is taking up space on your dropbox 👨💻? Find out how to check here.
Forum Discussion
brtdvrs
3 years agoHelpful | Level 5
i want to check is file exists with async/await
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
- Greg-DB3 years ago
Dropbox Community Moderator
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.
- brtdvrs3 years agoHelpful | Level 5I 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-DB3 years ago
Dropbox Community Moderator
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.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!