Take Your Search Game to the Next Level with Dropbox Dash 🚀✨ Curious how it works? Ask us here!
Forum Discussion
brtdvrs
3 years agoExplorer | Level 4
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
Sort By
- 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.
- brtdvrsExplorer | Level 4I 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.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.6,000 PostsLatest Activity: 5 days ago
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 or Facebook.
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!