Issue with retrieving metadata of file
Hi,
I'm using the Node.js SDK to interact with the Box API. I'm having an issue with consistency when retrieving metadata for a file.
The code snippet is at the bottom (I removed some code while making this post for privacy reasons, but I assure that it runs just fine). All it's doing is listening to events, and for any event of type ITEM_UPLOAD, it gets some string from the metadata and does some subsequent operations based on the string. For some reason, though, printing the metadata object in
const metadata = await client.files.getAllMetadata(event.source.id)
will return a file with no metadata, a file with just the name of the metadata template, OR a file with the name of the metadata template and the appropriate metadata (e.g., a "topicAreasCovered" attribute as part of the object). I'm not sure why it's inconsistent. The only way I've found to resolve this is adding a sleep command in the form of
await new Promise(r => setTimeout(r, 5000))
before trying to get the metadata. This returns all of the metadata just fine, but I'm not a fan of this solution.
Any help is appreciated.
Code snippet:
client.events.getEventStream((err, stream) => {
if (err) {
console.log(err)
}
// handle the event
stream.on('data', async (event) => {
switch (event.event_type) {
case 'ITEM_UPLOAD' : {
console.log(`file upload [${event.source.id}: ${event.source.name}] by ${event.created_by.name} at ${event.created_at}`)
//PROBLEM HERE------------------------------------
const metadata = await client.files.getMetadata(event.source.id, client.metadata.scopes.ENTERPRISE, METADATA_TEMPLATE)
//------------------------------------------------
const topic = metadata.topicAreasCovered[0]
const itemsInFolder = (await client.folders.getItems(
OUTPUT_FOLDER_ID,
{
usemarker: 'false',
fields: 'name',
offset: 0,
limit: 25
})
).entries
let folderExists = false
let folder
for (const item of itemsInFolder) {
folderExists = item.type === 'folder' && item.name === topic
if (folderExists) {
break
}
}
if (folderExists) {
folder = await client.folders.get(item.id)
} else {
folder = await client.folders.create(OUTPUT_FOLDER_ID, topic, () => console.log(`folder ${topic} created`))
}
await client.files.move(event.source.id, folder.id, () => console.log(`file ${event.source.name} moved to folder ${folder.name}`))
break
}
default: {
console.log('non file upload event occurred: ', event.event_type)
break
}
}
})
})
-
Seems to be better doing this:
let metadata = await client.files.getAllMetadata(event.source.id)
while (metadata.entries.length === 0 || !('topicAreasCovered' in metadata.entries[0])) {
await new Promise(r => setTimeout(r, 100)) //don't need this, but avoids console spam
metadata = await client.files.getAllMetadata(event.source.id)
console.log(metadata)
}but still not a great solution, I think. Does the file get uploaded before metadata is "attached" to the file? It seems strange that the metadata isn't associated immediately, so I'm wondering if it's just a timing, server-side related issue.
サインインしてコメントを残してください。
コメント
1件のコメント